﻿using Module_Debug.Hardware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;


namespace Module_Debug.VDDIC_Gamma
{
    public partial class RM692Fx : Form
    {
        #region 参数定义
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();
        //Class_JOJO_gamma Flexible = new Class_JOJO_gamma();
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        private static bool Gamma_adjust_timeout_flag = false;

        private ushort[] gamma_r = new ushort[33];
        private ushort[] gamma_g = new ushort[33];
        private ushort[] gamma_b = new ushort[33];

        //Gamma绑点定义
        // private byte[] Point_RM692Fx = new byte[33] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
        private int Total_points = 0;
        private int Frame_selected = 1;
        private int Gamma_selected = 1;
        private byte Gamma_selected_value = 1;
        private byte[] Gamma_selected_buffer = new byte[50]
            {1,2, 3, 4, 5, 6, 7, 8, 9, 10,
            11,12,13,14,15,16,17,18,19,20,
            21,22,23,24,25,26,27,28,29,30,
            31,32,33,34,35,36,37,38,39,40,
            81,82,0,  0, 0, 0, 0, 0, 0,83,};

        /// <summary>
        /// /////
        /// 
        private double auto_lum;  //  存放获取到的产品亮度值
        private double auto_x;
        private double auto_y;        //  存放获取到的产品亮度 色坐标 x,y
        private byte Gammnum;
        private double D_x, D_y;
        //private int index = 0;
        //private double lum_off, x_off, y_off;
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private uint[,] GammaValue = new uint[33, 3];//保存当前Gammavalue

        bool lastdirupR = true; bool lastdirupG = true; bool lastdirupB = true; // Gamma 调整方向
        private uint step_l = 1;   //Gamma 调节步长
        byte rstep = 16; byte gstep = 16; byte bstep = 16;
        bool STOPAutoGammaFlag = false;     //AutoGamma 停止标志位，解决定时器禁止后AutoGamma无法停止的问题
        byte[] GammaValueTeparr = new byte[54 * 3]; //存放格式化后的GammaValue，以便于直接下发至DDIC
        byte[] ReadBackarr = new byte[32];  //暂存下位机返回数据
        bool GammaReadBackFlag = false; // 下位机返回数据完成标志位
        byte[] GammaValueReceived = new byte[50 * 3]; //存放从下位机接收过来的GammaValue        
        bool ReceivOKFlag = false;
        int gammapoint_num_692Fx = 15;/////gamma绑点33个；
        private int[] RM692EX_Point = new int[15] {//---Gamma绑点数据，这里的数据仅供参考---
            0, 1, 3, 7, 15, 23, 31, 47, 63, 79, 111, 143, 207, 239, 255};
        //byte GPN =  (byte) RM692EX_Point.Length; // 获取Gamma绑点数量
        byte GPN = 0; //保存Gamma绑点数量
        System.DateTime TimeNow = new DateTime();
        /// </summary>

        public struct RM692Fx_in //接收区
        {
            public byte ID; //下位机识别ID
        }
        public RM692Fx_in RM692Fx_info = new RM692Fx_in();   //数据
        #endregion

        #region 窗体ID
        //RM692Fx 监听挂载
        frmMain frmHelp = null;
        public RM692Fx(frmMain RM692Fx)
        {
            frmHelp = RM692Fx;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体    
            RM692Fx_info.ID = 0X64;   //数据
        }
        //RM692Fx 监听卸载
        private void RM692Fx_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM692Fx_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.Child_window_hide_already = false;
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Fx_FormClosed！\r\n";
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void RM692Fx_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM692Fx_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Fx_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(RM692Fx_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Fx_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }
        #endregion

        #region 串口接收
        //串口接收
        private void RM692Fx_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                //byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // RM692Fx_info.ID: RM692Fx的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == RM692Fx_info.ID && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        //---------------------------------------------------RM692Fx---------------------------------------
                        if ((Rec_tmp[0] == RM692Fx_info.ID) || (Rec_tmp[0] == 0x64))
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n"; } break;
                                case 0x02:
                                    {
                                        int i;
                                        string tmp = "";
                                        for (i = 3; i <= (Rec_tmp[2] + 1); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);

                                        RM692Fx_Reg_Data.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x07:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x06:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma 绑点重置 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "inter gamma setting OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x09:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "exit setting OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma4 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写GOA OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x10:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        //RM692Fx_Time.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        //if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写第" + RM692Fx_Time.Text + "次\t写 OK！\r\n";
                                        //else if (Rec_tmp[4] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写第" + RM692Fx_Time.Text + "次\t写 OK！\r\n";
                                    }
                                    break;
                                case 0x20:
                                    {
                                        if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Change Gray\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Change GrayP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x1C:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0xAA:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GIR\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "GIR\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x04:
                                    {
                                        if (Setting.TempRecord.RM692EXCmdType == 0x03) //red
                                        {
                                            RM692Fx_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            RM692Fx_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            RM692Fx_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            RM692Fx_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            RM692Fx_R4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            RM692Fx_R5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            RM692Fx_R6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            RM692Fx_R7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            RM692Fx_R8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            RM692Fx_R9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            RM692Fx_R10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            RM692Fx_R11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            RM692Fx_R12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            RM692Fx_R13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            RM692Fx_R14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            if (start_15gamma == false)
                                            {
                                                RM692Fx_R15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM692Fx_R16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM692Fx_R17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM692Fx_R18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM692Fx_R19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM692Fx_R20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM692Fx_R21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM692Fx_R22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM692Fx_R23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM692Fx_R24.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM692Fx_R25.Value = Decimal.Parse((Rec_tmp[54] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM692Fx_R26.Value = Decimal.Parse((Rec_tmp[56] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                //RM692Fx_R27.Value = Decimal.Parse((Rec_tmp[58] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                //RM692Fx_R28.Value = Decimal.Parse((Rec_tmp[60] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                //RM692Fx_R29.Value = Decimal.Parse((Rec_tmp[62] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                //RM692Fx_R30.Value = Decimal.Parse((Rec_tmp[64] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                //RM692Fx_R31.Value = Decimal.Parse((Rec_tmp[66] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                                //RM692Fx_R32.Value = Decimal.Parse((Rec_tmp[68] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            }
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM692EXCmdType == 0x04) //green
                                        {
                                            RM692Fx_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            RM692Fx_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            RM692Fx_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            RM692Fx_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            RM692Fx_G4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            RM692Fx_G5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            RM692Fx_G6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            RM692Fx_G7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            RM692Fx_G8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            RM692Fx_G9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            RM692Fx_G10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            RM692Fx_G11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            RM692Fx_G12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            RM692Fx_G13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            RM692Fx_G14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            if (start_15gamma == false)
                                            {
                                                RM692Fx_G15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM692Fx_G16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM692Fx_G17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM692Fx_G18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM692Fx_G19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM692Fx_G20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM692Fx_G21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM692Fx_G22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM692Fx_G23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM692Fx_G24.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM692Fx_G25.Value = Decimal.Parse((Rec_tmp[54] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM692Fx_G26.Value = Decimal.Parse((Rec_tmp[56] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                //RM692Fx_G27.Value = Decimal.Parse((Rec_tmp[58] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                //RM692Fx_G28.Value = Decimal.Parse((Rec_tmp[60] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                //RM692Fx_G29.Value = Decimal.Parse((Rec_tmp[62] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                //RM692Fx_G30.Value = Decimal.Parse((Rec_tmp[64] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                //RM692Fx_G31.Value = Decimal.Parse((Rec_tmp[66] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                                //RM692Fx_G32.Value = Decimal.Parse((Rec_tmp[68] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            }
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM692EXCmdType == 0x05) //blue
                                        {
                                            RM692Fx_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            RM692Fx_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            RM692Fx_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            RM692Fx_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            RM692Fx_B4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            RM692Fx_B5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            RM692Fx_B6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            RM692Fx_B7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            RM692Fx_B8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            RM692Fx_B9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            RM692Fx_B10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            RM692Fx_B11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            RM692Fx_B12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            RM692Fx_B13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            RM692Fx_B14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            if (start_15gamma == false)
                                            {
                                                RM692Fx_B15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM692Fx_B16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM692Fx_B17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM692Fx_B18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM692Fx_B19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM692Fx_B20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM692Fx_B21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM692Fx_B22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM692Fx_B23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM692Fx_B24.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM692Fx_B25.Value = Decimal.Parse((Rec_tmp[54] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM692Fx_B26.Value = Decimal.Parse((Rec_tmp[56] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                //RM692Fx_B27.Value = Decimal.Parse((Rec_tmp[58] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                //RM692Fx_B28.Value = Decimal.Parse((Rec_tmp[60] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                //RM692Fx_B29.Value = Decimal.Parse((Rec_tmp[62] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                //RM692Fx_B30.Value = Decimal.Parse((Rec_tmp[64] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                //RM692Fx_B31.Value = Decimal.Parse((Rec_tmp[66] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                                //RM692Fx_B32.Value = Decimal.Parse((Rec_tmp[68] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            }
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                default: break;
                            }
                        }
                        //frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        //frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        //if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                        if (frmHelp.textBox1.Lines.Length > 500)
                        {
                            string path = System.Windows.Forms.Application.StartupPath + "\\LOG" + "\\log" + ".txt";
                            string path_bak = System.Windows.Forms.Application.StartupPath + "\\LOG" + "\\log_" +
                                                DateTime.Now.ToLongDateString().ToString() + "_" +
                                                DateTime.Now.Hour.ToString() + "_" +
                                                DateTime.Now.Minute.ToString() + "_" +
                                                DateTime.Now.Second.ToString() + ".txt";
                            if (false == System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\LOG"))
                            {
                                //创建pic文件夹
                                System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + "\\LOG");
                            }
                            if (!File.Exists(path))
                            {
                                FileInfo cymfile = new FileInfo(path);
                                FileStream fs = cymfile.Create();
                                fs.Close();
                            }

                            StreamWriter sw = File.AppendText(path);
                            sw.WriteLine(frmHelp.textBox1.Text);
                            sw.Flush();
                            sw.Close();

                            FileInfo cymfile1 = new FileInfo(path);
                            Int64 log_size = cymfile1.Length;// 文件字节大小
                            if (log_size > 20000000) { File.Move(path, path_bak); }
                            frmHelp.textBox1.Text = null;
                        }
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        #endregion

        #region GAMMA保存和载入
        //Gamma保存
        private void RM692Fx_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + RM692Fx_R0.Text + "," + "G0," + RM692Fx_G0.Text + "," + "B0," + RM692Fx_B0.Text + "\r\n";
                    str = str + "R1," + RM692Fx_R1.Text + "," + "G1," + RM692Fx_G1.Text + "," + "B1," + RM692Fx_B1.Text + "\r\n";
                    str = str + "R2," + RM692Fx_R2.Text + "," + "G2," + RM692Fx_G2.Text + "," + "B2," + RM692Fx_B2.Text + "\r\n";
                    str = str + "R3," + RM692Fx_R3.Text + "," + "G3," + RM692Fx_G3.Text + "," + "B3," + RM692Fx_B3.Text + "\r\n";
                    str = str + "R4," + RM692Fx_R4.Text + "," + "G4," + RM692Fx_G4.Text + "," + "B4," + RM692Fx_B4.Text + "\r\n";
                    str = str + "R5," + RM692Fx_R5.Text + "," + "G5," + RM692Fx_G5.Text + "," + "B5," + RM692Fx_B5.Text + "\r\n";
                    str = str + "R6," + RM692Fx_R6.Text + "," + "G6," + RM692Fx_G6.Text + "," + "B6," + RM692Fx_B6.Text + "\r\n";
                    str = str + "R7," + RM692Fx_R7.Text + "," + "G7," + RM692Fx_G7.Text + "," + "B7," + RM692Fx_B7.Text + "\r\n";
                    str = str + "R8," + RM692Fx_R8.Text + "," + "G8," + RM692Fx_G8.Text + "," + "B8," + RM692Fx_B8.Text + "\r\n";
                    str = str + "R9," + RM692Fx_R9.Text + "," + "G9," + RM692Fx_G9.Text + "," + "B9," + RM692Fx_B9.Text + "\r\n";
                    str = str + "R10," + RM692Fx_R10.Text + "," + "G10," + RM692Fx_G10.Text + "," + "B10," + RM692Fx_B10.Text + "\r\n";
                    str = str + "R11," + RM692Fx_R11.Text + "," + "G11," + RM692Fx_G11.Text + "," + "B11," + RM692Fx_B11.Text + "\r\n";
                    str = str + "R12," + RM692Fx_R12.Text + "," + "G12," + RM692Fx_G12.Text + "," + "B12," + RM692Fx_B12.Text + "\r\n";
                    str = str + "R13," + RM692Fx_R13.Text + "," + "G13," + RM692Fx_G13.Text + "," + "B13," + RM692Fx_B13.Text + "\r\n";
                    str = str + "R14," + RM692Fx_R14.Text + "," + "G14," + RM692Fx_G14.Text + "," + "B14," + RM692Fx_B14.Text + "\r\n";
                    str = str + "R15," + RM692Fx_R15.Text + "," + "G15," + RM692Fx_G15.Text + "," + "B15," + RM692Fx_B15.Text + "\r\n";
                    str = str + "R16," + RM692Fx_R16.Text + "," + "G16," + RM692Fx_G16.Text + "," + "B16," + RM692Fx_B16.Text + "\r\n";
                    str = str + "R17," + RM692Fx_R17.Text + "," + "G17," + RM692Fx_G17.Text + "," + "B17," + RM692Fx_B17.Text + "\r\n";
                    str = str + "R18," + RM692Fx_R18.Text + "," + "G18," + RM692Fx_G18.Text + "," + "B18," + RM692Fx_B18.Text + "\r\n";
                    str = str + "R19," + RM692Fx_R19.Text + "," + "G19," + RM692Fx_G19.Text + "," + "B19," + RM692Fx_B19.Text + "\r\n";
                    str = str + "R20," + RM692Fx_R20.Text + "," + "G20," + RM692Fx_G20.Text + "," + "B20," + RM692Fx_B20.Text + "\r\n";
                    str = str + "R21," + RM692Fx_R21.Text + "," + "G21," + RM692Fx_G21.Text + "," + "B21," + RM692Fx_B21.Text + "\r\n";
                    str = str + "R22," + RM692Fx_R22.Text + "," + "G22," + RM692Fx_G22.Text + "," + "B22," + RM692Fx_B22.Text + "\r\n";
                    str = str + "R23," + RM692Fx_R23.Text + "," + "G23," + RM692Fx_G23.Text + "," + "B23," + RM692Fx_B23.Text + "\r\n";
                    str = str + "R24," + RM692Fx_R24.Text + "," + "G24," + RM692Fx_G24.Text + "," + "B24," + RM692Fx_B24.Text + "\r\n";
                    str = str + "R25," + RM692Fx_R25.Text + "," + "G25," + RM692Fx_G25.Text + "," + "B25," + RM692Fx_B25.Text + "\r\n";
                    str = str + "R26," + RM692Fx_R26.Text + "," + "G26," + RM692Fx_G26.Text + "," + "B26," + RM692Fx_B26.Text + "\r\n";

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void RM692Fx_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                RM692Fx_R0.Text = arrtmp[1].ToString(); RM692Fx_G0.Text = arrtmp[3].ToString(); RM692Fx_B0.Text = arrtmp[5].ToString();
                RM692Fx_R1.Text = arrtmp[7].ToString(); RM692Fx_G1.Text = arrtmp[9].ToString(); RM692Fx_B1.Text = arrtmp[11].ToString();
                RM692Fx_R2.Text = arrtmp[13].ToString(); RM692Fx_G2.Text = arrtmp[15].ToString(); RM692Fx_B2.Text = arrtmp[17].ToString();
                RM692Fx_R3.Text = arrtmp[19].ToString(); RM692Fx_G3.Text = arrtmp[21].ToString(); RM692Fx_B3.Text = arrtmp[23].ToString();
                RM692Fx_R4.Text = arrtmp[25].ToString(); RM692Fx_G4.Text = arrtmp[27].ToString(); RM692Fx_B4.Text = arrtmp[29].ToString();
                RM692Fx_R5.Text = arrtmp[31].ToString(); RM692Fx_G5.Text = arrtmp[33].ToString(); RM692Fx_B5.Text = arrtmp[35].ToString();
                RM692Fx_R6.Text = arrtmp[37].ToString(); RM692Fx_G6.Text = arrtmp[39].ToString(); RM692Fx_B6.Text = arrtmp[41].ToString();
                RM692Fx_R7.Text = arrtmp[43].ToString(); RM692Fx_G7.Text = arrtmp[45].ToString(); RM692Fx_B7.Text = arrtmp[47].ToString();
                RM692Fx_R8.Text = arrtmp[49].ToString(); RM692Fx_G8.Text = arrtmp[51].ToString(); RM692Fx_B8.Text = arrtmp[53].ToString();
                RM692Fx_R9.Text = arrtmp[55].ToString(); RM692Fx_G9.Text = arrtmp[57].ToString(); RM692Fx_B9.Text = arrtmp[59].ToString();
                RM692Fx_R10.Text = arrtmp[61].ToString(); RM692Fx_G10.Text = arrtmp[63].ToString(); RM692Fx_B10.Text = arrtmp[65].ToString();
                RM692Fx_R11.Text = arrtmp[67].ToString(); RM692Fx_G11.Text = arrtmp[69].ToString(); RM692Fx_B11.Text = arrtmp[71].ToString();
                RM692Fx_R12.Text = arrtmp[73].ToString(); RM692Fx_G12.Text = arrtmp[75].ToString(); RM692Fx_B12.Text = arrtmp[77].ToString();
                RM692Fx_R13.Text = arrtmp[79].ToString(); RM692Fx_G13.Text = arrtmp[81].ToString(); RM692Fx_B13.Text = arrtmp[83].ToString();
                RM692Fx_R14.Text = arrtmp[85].ToString(); RM692Fx_G14.Text = arrtmp[87].ToString(); RM692Fx_B14.Text = arrtmp[89].ToString();
                RM692Fx_R15.Text = arrtmp[91].ToString(); RM692Fx_G15.Text = arrtmp[93].ToString(); RM692Fx_B15.Text = arrtmp[95].ToString();
                RM692Fx_R16.Text = arrtmp[97].ToString(); RM692Fx_G16.Text = arrtmp[99].ToString(); RM692Fx_B16.Text = arrtmp[101].ToString();
                RM692Fx_R17.Text = arrtmp[103].ToString(); RM692Fx_G17.Text = arrtmp[105].ToString(); RM692Fx_B17.Text = arrtmp[107].ToString();
                RM692Fx_R18.Text = arrtmp[109].ToString(); RM692Fx_G18.Text = arrtmp[111].ToString(); RM692Fx_B18.Text = arrtmp[113].ToString();
                RM692Fx_R19.Text = arrtmp[115].ToString(); RM692Fx_G19.Text = arrtmp[117].ToString(); RM692Fx_B19.Text = arrtmp[119].ToString();
                RM692Fx_R20.Text = arrtmp[121].ToString(); RM692Fx_G20.Text = arrtmp[123].ToString(); RM692Fx_B20.Text = arrtmp[125].ToString();
                RM692Fx_R21.Text = arrtmp[127].ToString(); RM692Fx_G21.Text = arrtmp[129].ToString(); RM692Fx_B21.Text = arrtmp[131].ToString();
                RM692Fx_R22.Text = arrtmp[133].ToString(); RM692Fx_G22.Text = arrtmp[135].ToString(); RM692Fx_B22.Text = arrtmp[137].ToString();
                RM692Fx_R23.Text = arrtmp[139].ToString(); RM692Fx_G23.Text = arrtmp[141].ToString(); RM692Fx_B23.Text = arrtmp[143].ToString();
                RM692Fx_R24.Text = arrtmp[145].ToString(); RM692Fx_G24.Text = arrtmp[147].ToString(); RM692Fx_B24.Text = arrtmp[149].ToString();
                RM692Fx_R25.Text = arrtmp[151].ToString(); RM692Fx_G25.Text = arrtmp[153].ToString(); RM692Fx_B25.Text = arrtmp[155].ToString();
                RM692Fx_R26.Text = arrtmp[157].ToString(); RM692Fx_G26.Text = arrtmp[159].ToString(); RM692Fx_B26.Text = arrtmp[161].ToString();
            }
        }
        #endregion

        private void RM692Fx_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[40];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 40;

            Send_buf[0] = 0x64;        //RM692Fx
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM692Fx_RegR":
                    {
                        string strLine = Convert.ToString(RM692Fx_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int DataNum = Convert.ToByte(tb_RM692Fx_return_num.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x64;
                        Send_buf[1] = 0x02; //read function
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register
                    }
                    break;
                case "RM692Fx_RegW":
                    {
                        string strLine = Convert.ToString(RM692Fx_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x64; //
                        Send_buf[1] = 0x01; //寄存器写
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                    }
                    break;
                case "btn_RM692Fx_gamma_point_select":
                    {
                        Send_buf[0] = 0x64;
                        Send_buf[1] = 0x06; //重置绑点位置
                        Send_buf[2] = 0x23; //33+2cmd = 35;
                    }
                    break;
                //case "RM692Fx_Reg_OTP":       //          B
                //    {
                //        Setting.TempRecord.cmdLen = 8;
                //        Send_buf = new byte[8];
                //        Send_buf[0] = 0x64;
                //        Send_buf[1] = 0x07; //OTP function
                //        Send_buf[2] = 3;//length
                //        if ((Convert.ToByte(RM692Fx_Time.Text) == 1)
                //            || (Convert.ToByte(RM692Fx_Time.Text) == 2)
                //            || (Convert.ToByte(RM692Fx_Time.Text) == 3)
                //            || (Convert.ToByte(RM692Fx_Time.Text) == 4)
                //            || (Convert.ToByte(RM692Fx_Time.Text) == 5)
                //            || (Convert.ToByte(RM692Fx_Time.Text) == 6))
                //        {
                //            ;
                //        }
                //        else
                //        {
                //            MessageBox.Show("otp_times设置错误，请检查", "错误");
                //            return;
                //        }
                //        Send_buf[3] = Convert.ToByte(RM692Fx_Time.Text);
                //        if ((radioButton_gamma.Checked) == true)
                //        {
                //            Send_buf[4] = 1;
                //            Gamma_selected_value = Gamma_selected_buffer[(Frame_selected - 1) * 10 + Gamma_selected - 1];
                //            Send_buf[5] = Convert.ToByte(Gamma_selected_value);
                //        }
                //        else if ((radioButton_All.Checked) == true)
                //        {
                //            Send_buf[4] = 5;
                //        }
                //        else if ((radioButton_RCN.Checked) == true)
                //        {
                //            Send_buf[4] = 6;
                //        }
                //        else if ((radioButton_Demura.Checked) == true)
                //        {
                //            Send_buf[4] = 7;
                //        }
                //    }
                //    break;
                case "button_inter_gamma":         //inter gamma setting
                    {
                        Setting.TempRecord.cmdLen = 8;
                        Send_buf = new byte[8];
                        Send_buf[0] = 0x64;
                        Send_buf[1] = 0x08;
                        Send_buf[2] = 3;//length
                        Send_buf[3] = Convert.ToByte(Gamma_Band_Select.Text);
                    }
                    break;

                case "button_exit_gamma":         //exit gamma setting
                    {
                        Setting.TempRecord.cmdLen = 8;
                        Send_buf = new byte[8];
                        Send_buf[0] = 0x64;
                        Send_buf[1] = 0x09;
                        Send_buf[2] = 3;//length
                        Send_buf[3] = Convert.ToByte(Gamma_Band_Select.Text);
                    }
                    break;
                case "btn_RM692Fx_GIR_SET":
                    {
                        Setting.TempRecord.cmdLen = 11;
                        Send_buf = new byte[11];
                        Send_buf[0] = 0x64;
                        Send_buf[1] = 0xAA;
                        Send_buf[2] = 6;//length
                        Send_buf[3] = (byte)(RM692Fx_GIR_26H.Value);
                        Send_buf[4] = (byte)(RM692Fx_GIR_27H.Value);
                        Send_buf[5] = (byte)(RM692Fx_GIR_R.Value);
                        Send_buf[6] = (byte)(RM692Fx_GIR_G.Value);
                        Send_buf[7] = (byte)(RM692Fx_GIR_B.Value);
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 3000;
            frmHelp.T1.Enabled = true;
        }
        private void RM692Fx_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            if (start_15gamma == false)
            {
                Send_buf = new byte[70];
                Setting.TempRecord.cmdLen = 70;
                Send_buf[2] = 69;
            }
            else
            {
                Send_buf = new byte[38];
                Setting.TempRecord.cmdLen = 38;
                Send_buf[2] = 33; //33
            }
            Send_buf[3] = 0;
            Send_buf[0] = 0x68;

            switch (btnTmp.Name)
            {
                case "RM692Fx_GammaR_R":
                    {
                        Setting.TempRecord.RM692EXCmdType = 0x03;       //      3
                        Send_buf[1] = 0x05;                             //读 Red Gamma
                        Send_buf[3] = 0x00;
                        Send_buf[4] = 0x01;
                    }
                    break;
                case "RM692Fx_GammaR_G":
                    {
                        Setting.TempRecord.RM692EXCmdType = 0x04;   //      4
                        Send_buf[1] = 0x05;                         //  读Gamma
                        Send_buf[3] = 0x01;                        //绿色 green
                        Send_buf[4] = 0x01;
                    }
                    break;
                case "RM692Fx_GammaR_B": //              9
                    {
                        Setting.TempRecord.RM692EXCmdType = 0x05;   //      5
                        Send_buf[1] = 0x05;                         //   读Gamma
                        Send_buf[3] = 0x02;                        //   blue
                        Send_buf[4] = 0x01;
                    }
                    break;
                //--------------------------------------
                case "RM692Fx_GammaW_R":
                    {
                        Send_buf[1] = 0x04;
                        if (radioButton_Page50.Checked == true)
                        { Send_buf[3] = 0x00; }
                        else if (radioButton_Page52.Checked == true)
                        { Send_buf[3] = 0x03; }
                        else
                        { Send_buf[3] = 0x04; }
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R0.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R0.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R1.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R1.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R2.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R2.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R3.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R3.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R4.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R4.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R5.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R5.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R6.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R6.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R7.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R7.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R8.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R8.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R9.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R9.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R10.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R10.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R11.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R11.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R12.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R12.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R13.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R13.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R14.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R14.Value.ToString())) & 0xff);
                        if (start_15gamma == false)
                        {
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692Fx_R26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692Fx_R26.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "RM692Fx_GammaW_G":
                    {
                        Send_buf[1] = 0x04;
                        if (radioButton_Page50.Checked == true)
                        { Send_buf[3] = 0x40; }
                        else if (radioButton_Page52.Checked == true)
                        { Send_buf[3] = 0x43; }
                        else
                        { Send_buf[3] = 0x44; }

                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G0.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G0.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G1.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G1.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G2.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G2.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G3.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G3.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G4.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G4.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G5.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G5.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G6.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G6.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G7.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G7.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G8.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G8.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G9.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G9.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G10.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G10.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G11.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G11.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G12.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G12.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G13.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G13.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G14.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G14.Value.ToString())) & 0xff);
                        if (start_15gamma == false)
                        {
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692Fx_G26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692Fx_G26.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "RM692Fx_GammaW_B":     //      
                    {
                        Send_buf[1] = 0x04;
                        if (radioButton_Page50.Checked == true)
                        { Send_buf[3] = 0x80; }
                        else if (radioButton_Page52.Checked == true)
                        { Send_buf[3] = 0x83; }
                        else
                        { Send_buf[3] = 0x84; }

                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B0.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B0.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B1.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B1.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B2.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B2.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B3.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B3.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B4.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B4.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B5.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B5.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B6.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B6.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B7.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B7.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B8.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B8.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B9.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B9.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B10.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B10.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B11.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B11.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B12.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B12.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B13.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B13.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B14.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B14.Value.ToString())) & 0xff);
                        if (start_15gamma == false)
                        {
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692Fx_B26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692Fx_B26.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                default:
                    break;
            }
            if (start_15gamma == false)
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[68], out Send_buf[69]);
            }
            else
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[36], out Send_buf[37]);
            }
            try
            {
                if (start_15gamma == false)
                {
                    frmHelp.comPort.Write(Send_buf, 0, 70);
                }
                else
                {
                    frmHelp.comPort.Write(Send_buf, 0, 38); //
                }
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }

        //jisuan
        private void btn_RM692Fx_Calculate_Click(object sender, EventArgs e)
        {
            // double Lr_d, Lg_d, Lb_d;
            double L_X, L_Y, L_Z;
            string[] arr = RM692Fx_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            //----------------------------------------------------------------------------
            try
            {
                txt2EX_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt2EX_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt2EX_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];
                txt2EX_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt2EX_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt2EX_X.Text = Diven_Gamma[0].X.ToString();
                txt2EX_Y.Text = Diven_Gamma[0].Y.ToString();
                Points_LV.Text = "";

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点       

                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    Points_LV.Text = Points_LV.Text + Diven_Offset[i].NO + ":  " + Math.Round(D_lum[i], 6) + "\r\n";
                    frmHelp.textBox1.Text += Diven_Offset[i].NO + ":  " + Math.Round(D_lum[i], 6) + "\r\n";////传输至主界面,便于查阅数值
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }

            btnCal_RM692Fx_Position_Click(sender, e);
        }

        public void DataGirdShow(byte index, double RRR, double GGG, double BBB)        //UI 表格显示 R/G/B对应Gamma寄存器值
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();

            Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.GammaReg_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.GammaReg_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.GammaReg_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.GammaReg_dataGridView.Rows.Add(Row1);
            }
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[0].Value = index + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[1].Value = (D_lum[index]).ToString("#0.0000");
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[2].Value = RRR;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[3].Value = GGG;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[4].Value = BBB;

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
                this.GammaReg_dataGridView.Rows[Current_Index].Selected = false;
                this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            GammaReg_dataGridView.FirstDisplayedScrollingRowIndex = GammaReg_dataGridView.RowCount - 1;
        }
        static byte[] GetCRCArray(byte[] arr, Byte speedflag)   //处理命令数组，在其头尾增加包头以及CRC校验值，处理后的数据可以直接发送至下位机
        {
            CRC16 CRCData = new CRC16();

            int arrNumbuer;                                                                                                                                                                                                                                                                                                                                             //51
            arrNumbuer = arr.Length;
            arrNumbuer += 6;
            byte[] Send_Datax = new byte[arrNumbuer];
            Send_Datax[0] = 0x9f;
            Send_Datax[1] = (byte)(speedflag << 4);
            Send_Datax[2] = Convert.ToByte(arr.Length + 1);  //为兼容下位机命令，此位置填待传输有效数据长度+1 

            Send_Datax[arr.Length + 3] = 0xFF;  //添加结束标志位
            arr.CopyTo(Send_Datax, 3);//合并数组头 4个Byte 标志位
            //计算校验码                                                 
            CRCData.CalculateCrc16(Send_Datax, out Send_Datax[arrNumbuer - 2], out Send_Datax[arrNumbuer - 1]);
            return Send_Datax;
        }
        private void GammaValueSet(byte rh, byte rl, byte gh, byte gl, byte bh, byte bl, int index_now, byte[] Gammaarr)  //把拆分后Gamma值按照DDIC 规定格式排列后送入DDIC
        {   //通过高速方式向第一条Gamma写入一组Gamma值
            byte[] TepArr; byte[] Gammaaddr_R; byte[] Gammaaddr_G; byte[] Gammaaddr_B; int temp_index = 0;
            /*
             RM692F0 gamma mapping
             */
            //瑞鼎Gamma R G B 地址突然就开始乱来了
            //                           00  00    01  01   02  02   03  03       04  04    05     05    06    06    07    07    08    08    09   09     0a
            Gammaaddr_R = new byte[] { 0x14, 0x15, 0x1A, 0x1B, 0x20, 0x21, 0x26, 0x27, 0x2d, 0x2f, 0x34, 0x35, 0x3a, 0x3b, 0x42, 0x43, 0x48, 0x49, 0x4e, 0x4f, 0x54, 0x55, 0x5b, 0x5c, 0x61, 0x62, 0x67, 0x68, 0x6d, 0x6e, 0x73, 0x74, 0x79, 0x7a, 0x7f, 0x80, 0x85, 0x86, 0x8b, 0x8c, 0x91, 0x92, 0x97, 0x98, 0x9d, 0x9e, 0xA4, 0xA5, 0xAB, 0xAC, 0xB1, 0xB2, 0xB7, 0xB8, 0xBD, 0xBE, 0xC3, 0xC4, 0xC9, 0xCA, 0xCF, 0xD0, 0xD5, 0xD6, 0xDB, 0xDC, };  //GammaR  31 绑点以下都是乱序的
            Gammaaddr_G = new byte[] { 0x16, 0x17, 0x1C, 0x1D, 0x22, 0x23, 0x28, 0x29, 0x30, 0x31, 0x36, 0x37, 0x3d, 0x3f, 0x44, 0x45, 0x4a, 0x4b, 0x50, 0x51, 0x56, 0x58, 0x5d, 0x5e, 0x63, 0x64, 0x69, 0x6a, 0x6f, 0x70, 0x75, 0x76, 0x7b, 0x7c, 0x81, 0x82, 0x87, 0x88, 0x8d, 0x8e, 0x93, 0x94, 0x99, 0x9a, 0x9f, 0xA0, 0xA6, 0xA7, 0xAD, 0xAE, 0xB3, 0xB4, 0xB9, 0xBA, 0xBF, 0xC0, 0xC5, 0xC6, 0xCB, 0xCC, 0xD1, 0xD2, 0xD7, 0xD8, 0xDD, 0xDE, };
            Gammaaddr_B = new byte[] { 0x18, 0x19, 0x1E, 0x1F, 0x24, 0x25, 0x2A, 0x2B, 0x32, 0x33, 0x38, 0x39, 0x40, 0x41, 0x46, 0x47, 0x4c, 0x4d, 0x52, 0x53, 0x59, 0x5a, 0x5f, 0x60, 0x65, 0x66, 0x6b, 0x6c, 0x71, 0x72, 0x77, 0x78, 0x7d, 0x7e, 0x83, 0x84, 0x89, 0x8a, 0x8f, 0x90, 0x95, 0x96, 0x9b, 0x9c, 0xA2, 0xA3, 0xA9, 0xAA, 0xAF, 0xB0, 0xB5, 0xB6, 0xBB, 0xBC, 0xC1, 0xC2, 0xC7, 0xC8, 0xCD, 0xCE, 0xD3, 0xD4, 0xD9, 0xDA, 0xDF, 0xE0, };

            /*
             RM692E0 gamma mapping
             */
            //Gammaaddr_R = new byte[] {0x01,0x02,0x48, 0x49, 0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51, 0x52 , 0x53, 0x54, 0x55, 0x56, //下面开始乱来
            //    0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66};  //GammaR  21 绑点以下都是按顺序的,我们这里只用了15个,所以懒得写了
            ////瑞鼎Gamma G B 地址突然就开始乱来了
            //Gammaaddr_G = new byte[] { 0x48, 0x49, 0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51, 0x52 , 0x53, 0x54, 0x55, 0x56, //下面开始乱来
            //    0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, };
            //Gammaaddr_B = new byte[] { 0x08d , 0x08e, 0x8f, 0x90, 0x091, 0x092, 0x093, 0x094, 0x095, 0x096, 0x097, 0x98, 0x099, 0x09a, 0x09b, 0x09c, 0x09d, 0x09e, 0x09f, 0xa0,
            //0xa2 ,0xa3 ,0xa4 ,0xa5 ,0xa6 ,0xa7 ,
            //0xa9 ,0xaa ,0xab ,0xac ,};


            try
            {
                //TepArr = new byte[] { 2, 0xfE, 50,
                //    4, 0XFF, 0XFF, 0XFF, 25,

                //    4, 0XFF, 0XFF, 0XFF, 15,

                //    4, 0XFF, 0XFF, 0XFF, 15 };  //byte[] TepArr;
                //if (chkAODMode.Checked)
                //{
                //    Gammnum = (byte)(numGammaPage.Value);
                //}
                //else if (!chkAutoSelectGammapage.Checked)
                //{
                //    Gammnum = (byte)(numGammaPage.Value);
                //}

                //TepArr[11] = Gammnum; //设定对应Gamma序号

                // Gammnum = (byte)(numGammaPage.Value);
                TepArr = new byte[] { 2, 0xfE, 0x50, };
                //Thread.Sleep(20);
                //Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾  
                Thread.Sleep(20);
                Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                //-----------------  
                TepArr = new byte[3];
                temp_index = GPN - index_now;
                //for (byte i = 0; i < temp_index * 2; i++)
                {
                    TepArr[0] = 2;
                    TepArr[1] = Gammaaddr_R[temp_index * 2 + 0];
                    TepArr[2] = Gammaarr[temp_index * 2 + 0];

                    frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }

                {
                    TepArr[0] = 2;
                    TepArr[1] = Gammaaddr_R[temp_index * 2 + 1];
                    TepArr[2] = Gammaarr[temp_index * 2 + 1];

                    frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }

                {
                    TepArr[0] = 2;
                    TepArr[1] = Gammaaddr_G[temp_index * 2 + 0];
                    TepArr[2] = Gammaarr[30 + temp_index * 2 + 0];

                    frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }
                {
                    TepArr[0] = 2;
                    TepArr[1] = Gammaaddr_G[temp_index * 2 + 1];
                    TepArr[2] = Gammaarr[30 + temp_index * 2 + 1];

                    frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }

                {
                    TepArr[0] = 2;
                    TepArr[1] = Gammaaddr_G[temp_index * 2 + 0];
                    TepArr[2] = Gammaarr[60 + temp_index * 2 + 0];

                    frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }
                {
                    TepArr[0] = 2;
                    TepArr[1] = Gammaaddr_G[temp_index * 2 + 1];
                    TepArr[2] = Gammaarr[60 + temp_index * 2 + 1];

                    frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                }
                //for (byte i = 0; i < gammapoint_num_692Fx * 2; i++)
                //{
                //    TepArr[3 * i + 0] = 2;
                //    TepArr[3 * i + 1] = Gammaaddr_R[i];
                //    TepArr[3 * i + 2] = Gammaarr[i];
                //}
                //frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                //Thread.Sleep(20);
                //Application.DoEvents();//重点，必须加上，否则父子窗体都假死

                //for (byte i = 0; i < gammapoint_num_692Fx * 2; i++)
                //{
                //    TepArr[3 * i + 0] = 2;
                //    TepArr[3 * i + 1] = Gammaaddr_G[i];
                //    TepArr[3 * i + 2] = Gammaarr[i + 30];
                //}
                //frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                //Thread.Sleep(20);
                //Application.DoEvents();//重点，必须加上，否则父子窗体都假死

                //TepArr = new byte[gammapoint_num_692Fx * 2 * 3];
                //for (byte i = 0; i < gammapoint_num_692Fx * 2; i++)
                //{
                //    TepArr[3 * i + 0] = 2;
                //    TepArr[3 * i + 1] = Gammaaddr_B[i];
                //    TepArr[3 * i + 2] = Gammaarr[i + 60];
                //}
                //frmHelp.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                //Thread.Sleep(20);
                //Application.DoEvents();//重点，必须加上，否则父子窗体都假死


            }
            catch
            {
                frmHelp.textBox1.Text += "Gamma无法写入，请检查" + "'\r\n";
                //txttip.Focus();//获取焦点
                //txttip.Select(txttip.TextLength, 0);//光标定位到文本最后
                //txttip.ScrollToCaret();//滚动到光标处
                //txttip.ForeColor = Color.OrangeRed;
                return;
            }
        }
        private void GammaValueFormat_Click(object sender, EventArgs e)   //GammaValue 值拆分成 Byte 并与实际地址进行转换 以便于后续处理发送至下位机
        {
            for (int i = 0; i < gammapoint_num_692Fx; i++)
            {
                //Gamma R
                GammaValueTeparr[2 * i + 0] = (byte)(GammaValue[i, 0] >> 8);
                GammaValueTeparr[2 * i + 1] = (byte)(GammaValue[i, 0] & 0xff);
                ////Gamma G
                //GammaValueTeparr[6 * i + 2] = (byte)(GammaValue[i, 1] >> 8);
                //GammaValueTeparr[6 * i + 3] = (byte)(GammaValue[i, 1] & 0xff);
                ////Gamma B
                //GammaValueTeparr[6 * i + 4] = (byte)(GammaValue[i, 2] >> 8);
                //GammaValueTeparr[6 * i + 5] = (byte)(GammaValue[i, 2] & 0xff);
            }
            for (int i = 0; i < gammapoint_num_692Fx; i++)
            {
                //Gamma G
                GammaValueTeparr[30 + 2 * i + 0] = (byte)(GammaValue[i, 1] >> 8);
                GammaValueTeparr[30 + 2 * i + 1] = (byte)(GammaValue[i, 1] & 0xff);
            }
            for (int i = 0; i < gammapoint_num_692Fx; i++)
            {
                //Gamma B
                GammaValueTeparr[60 + 2 * i + 0] = (byte)(GammaValue[i, 2] >> 8);
                GammaValueTeparr[60 + 2 * i + 1] = (byte)(GammaValue[i, 2] & 0xff);
            }
        }
        private void btnWriteGamma_Click(object sender, EventArgs e)   //检查GammaValue 并且重新转换成指定格式后写入DDIC
        {
            CheckandCorrectGammaValue();   //检查Gamma 是否存在高低灰阶数值错乱 ,并刷新表格数据              
            GammaValueFormat_Click(null, null);  //Gamma 值与实际地址进行转换 
            GammaValueSet(0, 0, 0, 0, 0, 0, GPN, GammaValueTeparr); //GammaValue 转换成特定格式后再通过此函数写入DDIC
        }
        uint GammaValueMax = 4096 - 1;
        private void CheckandCorrectGammaValue()   //检查GammaValue 是否按照大小排列
        {
            byte i; GPN = (byte)RM692EX_Point.Length; // 获取Gamma绑点数量
            if (GammaValue[GPN - 1, 0] > GammaValueMax)
            {
                GammaValue[GPN - 1, 0] = GammaValueMax - 16;
            }
            if (GammaValue[GPN - 1, 1] > GammaValueMax)
            {
                GammaValue[GPN - 1, 1] = GammaValueMax - 16;
            }
            if (GammaValue[GPN - 1, 2] > GammaValueMax)
            {
                GammaValue[GPN - 1, 2] = GammaValueMax - 16;
            }  //确保GammaValue不要超过DDIC 中Gamma寄存能接受的最大值


            for (i = (byte)(GPN - 1); i > 0; i--)    //低灰阶GammaValue如果大于高灰阶GammaValue则强制变小
            {
                if (GammaValue[i, 0] < GammaValue[i - 1, 0])
                {
                    GammaValue[i - 1, 0] = GammaValue[i, 0] - 0;
                    //GammaValueFormat_Click(null, null);
                }
                if (GammaValue[i, 1] < GammaValue[i - 1, 1])
                {
                    GammaValue[i - 1, 1] = GammaValue[i, 1] - 0;
                    //GammaValueFormat_Click(null, null);
                }
                if (GammaValue[i, 2] < GammaValue[i - 1, 2])
                {
                    GammaValue[i - 1, 2] = GammaValue[i, 2] - 0;
                    //GammaValueFormat_Click(null, null);
                }
            }
            GammaValue[0, 0] = 0; GammaValue[0, 1] = 0; GammaValue[0, 2] = 0;
            //if (chkGammaGridViewFresh.Checked)   // 刷新UI界面GammaValue值显示
            {
                this.GammaReg_dataGridView.Rows.Clear();
                for (i = 0; i < GPN; i++)
                {
                    DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                }
            }
        }
        private void btnGammaValueUpdate_Click(object sender, EventArgs e)      //刷新UI上表格内的Gamma数据
        {
            int Line_Count = 0;
            try
            {
                Line_Count = GammaReg_dataGridView.RowCount;
            }
            catch
            {
                MessageBox.Show("表格数据(行数)异常！", "提示：");
                return;
            }
            try
            {
                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                {
                    GammaValue[i, 0] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[2].Value);
                    GammaValue[i, 1] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[3].Value);
                    GammaValue[i, 2] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[4].Value);
                }
                //MessageBox.Show("数据更新OK！", "提示：");
                frmHelp.textBox1.Text += "Gamma数据更新OK！\r\n";
                btnWriteGamma.PerformClick();
            }
            catch
            {
                MessageBox.Show("Gamma数据更新错误！", "唐韬说：");
            }
        }
        private void numStep_ValueChanged(object sender, EventArgs e)   //手动调整AutoGamma调节步长以在初次调整Gamma是提升速度
        {
            rstep = (byte)(int.Parse(numStep.Value.ToString()));
            gstep = (byte)(int.Parse(numStep.Value.ToString()));
            bstep = (byte)(int.Parse(numStep.Value.ToString()));
        }

        private void btnCal_RM692Fx_Position_Click(object sender, EventArgs e)
        {
            lb_RM692Fx_R0.Items[0] = lb_RM692Fx_G0.Items[0] = lb_RM692Fx_B0.Items[0] = Diven_Offset[14 - 0].NO;
            lb_RM692Fx_R0.Items[1] = lb_RM692Fx_G0.Items[1] = lb_RM692Fx_B0.Items[1] = Diven_Offset[14 - 1].NO;
            lb_RM692Fx_R0.Items[2] = lb_RM692Fx_G0.Items[2] = lb_RM692Fx_B0.Items[2] = Diven_Offset[14 - 2].NO;
            lb_RM692Fx_R0.Items[3] = lb_RM692Fx_G0.Items[3] = lb_RM692Fx_B0.Items[3] = Diven_Offset[14 - 3].NO;
            lb_RM692Fx_R0.Items[4] = lb_RM692Fx_G0.Items[4] = lb_RM692Fx_B0.Items[4] = Diven_Offset[14 - 4].NO;
            lb_RM692Fx_R0.Items[5] = lb_RM692Fx_G0.Items[5] = lb_RM692Fx_B0.Items[5] = Diven_Offset[14 - 5].NO;
            lb_RM692Fx_R0.Items[6] = lb_RM692Fx_G0.Items[6] = lb_RM692Fx_B0.Items[6] = Diven_Offset[14 - 6].NO;

            lb_RM692Fx_R1.Items[0] = lb_RM692Fx_G1.Items[0] = lb_RM692Fx_B1.Items[0] = Diven_Offset[14 - 7].NO;
            lb_RM692Fx_R1.Items[1] = lb_RM692Fx_G1.Items[1] = lb_RM692Fx_B1.Items[1] = Diven_Offset[14 - 8].NO;
            lb_RM692Fx_R1.Items[2] = lb_RM692Fx_G1.Items[2] = lb_RM692Fx_B1.Items[2] = Diven_Offset[14 - 9].NO;
            lb_RM692Fx_R1.Items[3] = lb_RM692Fx_G1.Items[3] = lb_RM692Fx_B1.Items[3] = Diven_Offset[14 - 10].NO;
            lb_RM692Fx_R1.Items[4] = lb_RM692Fx_G1.Items[4] = lb_RM692Fx_B1.Items[4] = Diven_Offset[14 - 11].NO;
            lb_RM692Fx_R1.Items[5] = lb_RM692Fx_G1.Items[5] = lb_RM692Fx_B1.Items[5] = Diven_Offset[14 - 12].NO;
            lb_RM692Fx_R1.Items[6] = lb_RM692Fx_G1.Items[6] = lb_RM692Fx_B1.Items[6] = Diven_Offset[14 - 13].NO;

            lb_RM692Fx_R2.Items[0] = lb_RM692Fx_G2.Items[0] = lb_RM692Fx_B2.Items[0] = Diven_Offset[14 - 14].NO;
        }
        private void Load_GammmPic(string filenames)      //双击 载入 BMP图片
        {
            Int32 HACT_True = 0;
            Int32 HACT_Skip = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            FileStream aFile;

            //byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent = new byte[512 * 4000 + 100];
            System.Int32 ByteReaded = 0x00;

            string FileName = Gamma_loading_path.Text + '\\' + filenames + ".bmp";
            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);

            HACT_Skip = HACT_True;
            try
            { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                frmHelp.comPort.Write(TemFileContent, 0, 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            //-----------------------------------
            try
            {
                j = 1;
                aFile.Seek(54, SeekOrigin.Begin);
                while (j <= VACT_True)
                {
                    if (HACT_True % 4 != 0)
                        aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                    else
                        aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

                    ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                    try
                    {
                        frmHelp.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        break;
                    }
                    m_txlen += ByteReaded;
                    j++;
                }

                TemFileContent[0] = 0x5A;
                try
                {
                    frmHelp.comPort.Write(TemFileContent, 0, 1);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                aFile.Close();
            }
            catch
            {
                frmHelp.textBox1.Text += "啥图片都没显示";
                frmHelp.textBox1.Select(frmHelp.textBox1.Text.Length, 0);
                frmHelp.textBox1.ScrollToCaret();
            }
        }
        private static byte Gamma_Auto_flag_dll = 0;
        private int Multi_gamma_txt_now_index = 0;
        private int Multi_gamma_txt_next_index;
        private int Multi_gamma_txt_line_count = 0;
        private int Multi_gamma_group_index = 0;
        private void Btn_flexible_gamma_Click(object sender, EventArgs e)
        {
            RM692Fx_GIR_State = 0; ;
            btn_RM692Fx_Calculate_Click(null, null);
            Multi_gamma_txt_now_index = 0;
            Multi_gamma_txt_line_count = this.RM692Fx_Gamma_SetBox.Lines.GetUpperBound(0);
            Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
        }
        private void btnAutoGamma_RM692Fx_Click(object sender, EventArgs e)//255阶为例
        {
            double L_X, L_Y, L_Z;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------

            gamma_r = GAMMAs.RM692EX_Gamma_r;
            gamma_g = GAMMAs.RM692EX_Gamma_g;
            gamma_b = GAMMAs.RM692EX_Gamma_b;
            if (cBx_AOD_Auto_Gamma.Checked == true)  //AOD gamma adjust in AOD mode or not
            {
                Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
            }
            else
            {
                frmHelp.numPatternR.Value = Diven_Offset[index].NO;
                frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);
            }
            Thread.Sleep(50);
            //T17.Enabled = true;
            T20.Enabled = true;
        }

        private void btnSaveGamma_Click(object sender, EventArgs e)
        {
            string tepGammaR = ""; string tepGammaG = ""; string tepGammaB = "";
            string tepstr = "";
            string vtdrGammatxt = "";
            Int16 gdattmp;
            {
                //Button btnTmp = (Button)sender;
                //switch (btnTmp.Name)
                {
                    //    case "btnSaveGamma":
                    {
                        //以文本形式保存

                        {//保存
                            SaveFileDialog sf = new SaveFileDialog(); //对话框
                            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                            string str = "灰阶序号,灰阶亮度,reg_R,reg_G,reg_B\r\n";
                            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                            {
                                if (sf.FileName == "") return; //如果没有输入文件名
                                try
                                {
                                    StreamWriter sw = null; //文件流
                                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                    for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                    {
                                        str = str + this.GammaReg_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                            + this.GammaReg_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                            + this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                            + this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString() + ","
                                            + this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString() + "\r\n";
                                    }
                                    sw.WriteLine(str);
                                    sw.Dispose(); //释放资源
                                    sw.Close(); //关闭数据流
                                    MessageBox.Show("数据保存OK！", "系统提示：");

                                }
                                catch
                                {
                                    MessageBox.Show("数据保存时发生错误，请再试！", "系统提示：");
                                }
                            }

                        }
                    }
                    //        break;
                    ////    case "btnLoadGamma":
                    //        {
                    //            this.GammaReg_dataGridView.Rows.Clear();
                    //            OpenFileDialog sf = new OpenFileDialog();
                    //            int Line_Count = 0;
                    //            string strtmp = "";
                    //            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                    //            sf.Title = "数据导入";
                    //            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                    //            {
                    //                FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                    //                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                    //                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                    //                string strLine = streamReader.ReadLine();
                    //                while (!string.IsNullOrEmpty(strLine))
                    //                {
                    //                    strtmp = strtmp + strLine + ",";
                    //                    strLine = streamReader.ReadLine(); //循环读取下一行
                    //                    Line_Count++;
                    //                }
                    //                fs.Close(); //关闭流
                    //                streamReader.Close();
                    //                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                    //                for (int j = 1; j <= Line_Count - 1; j++)
                    //                {
                    //                    DataGridViewRow Row1 = new DataGridViewRow();
                    //                    GammaReg_dataGridView.Rows.Add(Row1);
                    //                }
                    //                try
                    //                {
                    //                    for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                    //                    {
                    //                        GammaReg_dataGridView.Rows[i].Cells[0].Value = arrtmp[5 * (i + 1) + 0];
                    //                        GammaReg_dataGridView.Rows[i].Cells[1].Value = arrtmp[5 * (i + 1) + 1];
                    //                        GammaReg_dataGridView.Rows[i].Cells[2].Value = arrtmp[5 * (i + 1) + 2];
                    //                        GammaReg_dataGridView.Rows[i].Cells[3].Value = arrtmp[5 * (i + 1) + 3];
                    //                        GammaReg_dataGridView.Rows[i].Cells[4].Value = arrtmp[5 * (i + 1) + 4];
                    //                        //------------------------------------
                    //                        GammaValue[i, 0] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 2]);
                    //                        GammaValue[i, 1] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 3]);
                    //                        GammaValue[i, 2] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 4]);
                    //                    }
                    //                    MessageBox.Show("数据导入OK！", "提示：");
                    //                }
                    //                catch
                    //                {
                    //                    MessageBox.Show("数据导入时发生错误！", "提示：");
                    //                }
                    //            }
                    //        }
                    //       break;
                    ////    case "btn_CAS200_Clean":
                    //        {
                    //            DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                    //            if (dr == DialogResult.OK)
                    //            {
                    //                this.GammaReg_dataGridView.Rows.Clear();
                    //            }
                    //        }
                    // //       break;
                    //
                    //    default: break;
                }
            }
        }


        private void btnCalGammaValue_Click(object sender, EventArgs e)   //计算对应绑点灰阶值，并连同暂存的Gammavale显示在窗体表格上
        {
            GPN = (byte)RM692EX_Point.Length; // 获取Gamma绑点数量
            D_lum = new double[GPN];
            {
                this.GammaReg_dataGridView.Rows.Clear();
            }
            for (byte i = 0; i < GPN; i++)
            {
                D_lum[i] = (Math.Pow((RM692EX_Point[i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
                DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
            }
        }


        private void btnAutoGammas_RM692EX_Click(object sender, EventArgs e)
        {
            int flag = 1;
            numStep.Maximum = 256;
            //txttip.ForeColor = Color.Black;
            STOPAutoGammaFlag = false;
            GPN = (byte)RM692EX_Point.Length; // 获取Gamma绑点数量
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[GPN, 3];
            //D_lum = new double[GPN];
            index = 1;  //Gamma绑点序号，第一个绑点通常对应255灰阶
            flag1 = flag2 = flag3 = 1;
            rstep = 16;
            gstep = 16;
            bstep = 16;
            frmHelp.textBox1.Text += "GammaValue最大值被限定为: " + GammaValueMax + "\r\n";
            //TimTest.Start();
            TimeNow = DateTime.Now; //开始计时

            //for (int i = 0; i < GPN; i++)  //计算相应绑点对应的亮度值
            //{
            //    D_lum[i] = (Math.Pow((RM692EX_Point[i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
            //}

            D_x = Convert.ToDouble(txt2EX_X.Text);  //获取调节目标色坐标 x
            D_y = Convert.ToDouble(txt2EX_Y.Text); ////获取调节目标色坐标 y
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < GPN; i++)   // 通过色坐标已经亮度计算各绑点R/G/B分量对应的亮度值
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }
            labTargetlum.Text = "Tar:" + D_lum[GPN - index].ToString("f4"); ;   //显示当前绑点对应的目标亮度值
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        //lum_off = Convert.ToDouble(txtVT_loff.Text);
                        x_off = Convert.ToDouble(txt2EX_Xoff.Text);   //获取色坐标允差范围
                        y_off = Convert.ToDouble(txt2EX_Yoff.Text);
                        frmHelp.numPatternR.Value = RM692EX_Point[GPN - index];     //设定绑点灰阶
                                                                                    //numPatternG.Value = RM692EX_Point[GPN - index];
                                                                                    //numPatternB.Value = RM692EX_Point[GPN - index];

                        //if (chkTurnsubGamma.Checked)
                        //{
                        //    btnPrevPic_Click(null, null);
                        //}
                        //else
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);       //显示255白画面，从255白画面开始调节

                        Thread.Sleep(10);
                        frmHelp.Msr_CASDK();
                        //Msr_CA310(); //修复讨厌的灰阶跳变时CA410数据异常的问题
                        ReceivOKFlag = false;
                        //T17.Enabled = true;   //通过T17定时器进行Gamma调整
                        T20.Enabled = true;   //通过T20定时器进行Gamma调整
                    }
                    break;
                case 2:  //寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private void btnVTStop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            //Tim_GirAdjust.Enabled = false;
            STOPAutoGammaFlag = true;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            T17.Enabled = false;//停止计时
            //txttip.ForeColor = Color.Black;
            btnCalculateRM692EX.PerformClick();
        }
        private void Multi_flexible_gammas(int txt_index, out int list_view_index)
        {
            string strLine = Convert.ToString(RM692Fx_Gamma_SetBox.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组
            string perline = string.Empty;
            string perline0 = string.Empty;
            list_view_index = 0;

            if (RM692Fx_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else  //标记关键事件
            {
                try
                {
                    arr = strLine.Split('\n'); //以逗号分割，添加到数
                    for (int i = txt_index; i < arr.Length;)  //获取的行数
                    {
                        perline = arr[i];
                        perline = perline.Replace(" ", "");
                        perline = perline.Replace(";", "");
                        perline = perline.Replace("(", "");
                        perline = perline.Replace(")", "");
                        perline = perline.Replace("\r", "");
                        perline = perline.ToUpper();

                        if (perline.Contains("REGW"))
                        {
                            perline = perline.Replace("REGW", "");
                            RM692Fx_Reg_Data.Text = perline;
                            RM692Fx_Others_Click(RM692Fx_RegW, null);
                            Thread.Sleep(200);
                            i = i + 1;
                        }
                        else if (perline.Contains("AOD_IN"))
                        {
                            //   cBx_AOD_Auto_Gamma.Checked = true;
                            //  i = i + 1;
                        }
                        else if (perline.Contains("AOD_OUT"))
                        {
                            //  cBx_AOD_Auto_Gamma.Checked = false;
                            // i = i + 1;
                        }
                        else if (perline.Contains("SAVE"))
                        {
                            string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                            if (Directory.Exists(pathsave_file) == false)
                            {
                                Directory.CreateDirectory(pathsave_file);
                            }
                            string pathsave = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                            Multi_SaveGamma(pathsave);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("GAMMA_SET"))
                        {
                            frmHelp.cbxlogic_go.Checked = false;
                            perline = perline.Replace("GAMMA_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_RM692Fx_Normal.Checked = true;
                            //RM692Fx_Inter_Gamma_Click(null, null);
                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();


                            }
                            catch {; }
                            if (Total_points > 15)
                            {
                                //  rbt_VT6110_P25.Checked = true;
                            }
                            else
                            {
                                // rbt_VT6110_IRC.Checked = true;
                            }
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_R, null);
                            Thread.Sleep(200);
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_G, null);
                            Thread.Sleep(200);
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_B, null);
                            Thread.Sleep(200);
                            btnAutoGamma_RM692Fx_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            list_view_index = i;
                            break;
                        }
                        else if (perline.Contains("UDC_SET"))
                        {
                            frmHelp.cbxlogic_go.Checked = true;
                            perline = perline.Replace("UDC_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            //rBt_RM692Fx_UDC.Checked = true;
                            RM692Fx_Inter_Gamma_Click(null, null);
                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();

                                RM692Fx_Gamma_Click(RM692Fx_GammaW_R, null);
                                Thread.Sleep(20);
                                RM692Fx_Gamma_Click(RM692Fx_GammaW_G, null);
                                Thread.Sleep(20);
                                RM692Fx_Gamma_Click(RM692Fx_GammaW_B, null);
                                Thread.Sleep(20);
                            }
                            catch {; }
                            if (Total_points > 15)
                            {
                                //  rbt_VT6110_P25.Checked = true;
                            }
                            else
                            {
                                // rbt_VT6110_IRC.Checked = true;
                            }
                            btnAutoGamma_RM692Fx_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            list_view_index = i;
                            break;
                        }
                        else if (perline.Contains("GAMMA_CAL"))
                        {
                            perline = perline.Replace("GAMMA_CAL", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_RM692Fx_Normal.Checked = true;
                            RM692Fx_Inter_Gamma_Click(null, null);
                            UDC694_Gamma_Linear_interpolation(Convert.ToInt32(Gamma_Band_Select.Text));

                            RM692Fx_Gamma_Click(RM692Fx_GammaW_R, null);
                            Thread.Sleep(20);
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_G, null);
                            Thread.Sleep(20);
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_B, null);
                            Thread.Sleep(20);
                            list_view_index = i;
                        }
                        else if (perline.Contains("UDC_CAL"))
                        {
                            perline = perline.Replace("UDC_CAL", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            //rBt_RM692Fx_UDC.Checked = true;
                            RM692Fx_Inter_Gamma_Click(null, null);
                            UDC694_Gamma_Linear_interpolation(Convert.ToInt32(Gamma_Band_Select.Text));

                            RM692Fx_Gamma_Click(RM692Fx_GammaW_R, null);
                            Thread.Sleep(20);
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_G, null);
                            Thread.Sleep(20);
                            RM692Fx_Gamma_Click(RM692Fx_GammaW_B, null);
                            Thread.Sleep(20);
                            list_view_index = i;
                        }

                        else if (perline.Contains("EXITGAMMA"))
                        {
                            RM692Fx_Exit_Gamma_Click(null, null);
                            i = i + 1;
                        }
                        else
                        {
                            i = i + 1;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("输入格式有误！");
                }
            }
        }
        private void Multi_SaveGamma(string path)
        {
            try
            {
                string str = "保存Gamma文件";
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(path, false, Encoding.UTF8);
                str = str + "R0," + RM692Fx_R0.Text + "," + "G0," + RM692Fx_G0.Text + "," + "B0," + RM692Fx_B0.Text + "\r\n";
                str = str + "R1," + RM692Fx_R1.Text + "," + "G1," + RM692Fx_G1.Text + "," + "B1," + RM692Fx_B1.Text + "\r\n";
                str = str + "R2," + RM692Fx_R2.Text + "," + "G2," + RM692Fx_G2.Text + "," + "B2," + RM692Fx_B2.Text + "\r\n";
                str = str + "R3," + RM692Fx_R3.Text + "," + "G3," + RM692Fx_G3.Text + "," + "B3," + RM692Fx_B3.Text + "\r\n";
                str = str + "R4," + RM692Fx_R4.Text + "," + "G4," + RM692Fx_G4.Text + "," + "B4," + RM692Fx_B4.Text + "\r\n";
                str = str + "R5," + RM692Fx_R5.Text + "," + "G5," + RM692Fx_G5.Text + "," + "B5," + RM692Fx_B5.Text + "\r\n";
                str = str + "R6," + RM692Fx_R6.Text + "," + "G6," + RM692Fx_G6.Text + "," + "B6," + RM692Fx_B6.Text + "\r\n";
                str = str + "R7," + RM692Fx_R7.Text + "," + "G7," + RM692Fx_G7.Text + "," + "B7," + RM692Fx_B7.Text + "\r\n";
                str = str + "R8," + RM692Fx_R8.Text + "," + "G8," + RM692Fx_G8.Text + "," + "B8," + RM692Fx_B8.Text + "\r\n";
                str = str + "R9," + RM692Fx_R9.Text + "," + "G9," + RM692Fx_G9.Text + "," + "B9," + RM692Fx_B9.Text + "\r\n";
                str = str + "R10," + RM692Fx_R10.Text + "," + "G10," + RM692Fx_G10.Text + "," + "B10," + RM692Fx_B10.Text + "\r\n";
                str = str + "R11," + RM692Fx_R11.Text + "," + "G11," + RM692Fx_G11.Text + "," + "B11," + RM692Fx_B11.Text + "\r\n";
                str = str + "R12," + RM692Fx_R12.Text + "," + "G12," + RM692Fx_G12.Text + "," + "B12," + RM692Fx_B12.Text + "\r\n";
                str = str + "R13," + RM692Fx_R13.Text + "," + "G13," + RM692Fx_G13.Text + "," + "B13," + RM692Fx_B13.Text + "\r\n";
                str = str + "R14," + RM692Fx_R14.Text + "," + "G14," + RM692Fx_G14.Text + "," + "B14," + RM692Fx_B14.Text + "\r\n";
                str = str + "R15," + RM692Fx_R15.Text + "," + "G15," + RM692Fx_G15.Text + "," + "B15," + RM692Fx_B15.Text + "\r\n";
                str = str + "R16," + RM692Fx_R16.Text + "," + "G16," + RM692Fx_G16.Text + "," + "B16," + RM692Fx_B16.Text + "\r\n";
                str = str + "R17," + RM692Fx_R17.Text + "," + "G17," + RM692Fx_G17.Text + "," + "B17," + RM692Fx_B17.Text + "\r\n";
                str = str + "R18," + RM692Fx_R18.Text + "," + "G18," + RM692Fx_G18.Text + "," + "B18," + RM692Fx_B18.Text + "\r\n";
                str = str + "R19," + RM692Fx_R19.Text + "," + "G19," + RM692Fx_G19.Text + "," + "B19," + RM692Fx_B19.Text + "\r\n";
                str = str + "R20," + RM692Fx_R20.Text + "," + "G20," + RM692Fx_G20.Text + "," + "B20," + RM692Fx_B20.Text + "\r\n";
                str = str + "R21," + RM692Fx_R21.Text + "," + "G21," + RM692Fx_G21.Text + "," + "B21," + RM692Fx_B21.Text + "\r\n";
                str = str + "R22," + RM692Fx_R22.Text + "," + "G22," + RM692Fx_G22.Text + "," + "B22," + RM692Fx_B22.Text + "\r\n";
                str = str + "R23," + RM692Fx_R23.Text + "," + "G23," + RM692Fx_G23.Text + "," + "B23," + RM692Fx_B23.Text + "\r\n";
                str = str + "R24," + RM692Fx_R24.Text + "," + "G24," + RM692Fx_G24.Text + "," + "B24," + RM692Fx_B24.Text + "\r\n";
                str = str + "R25," + RM692Fx_R25.Text + "," + "G25," + RM692Fx_G25.Text + "," + "B25," + RM692Fx_B25.Text + "\r\n";
                str = str + "R26," + RM692Fx_R26.Text + "," + "G26," + RM692Fx_G26.Text + "," + "B26," + RM692Fx_B26.Text + "\r\n";
                sw.WriteLine(str);
                sw.Dispose(); //释放资源
                sw.Close(); //关闭数据流
            }
            catch
            {
                MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
            }
        } //
        private void Text_Processing(string[] txt_path)
        {
            double L_X, L_Y, L_Z;
            string[] arr = txt_path;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            try  //获取调整spec
            {
                txt2EX_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt2EX_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt2EX_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txt2EX_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt2EX_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt2EX_X.Text = Diven_Gamma[0].X.ToString();
                txt2EX_Y.Text = Diven_Gamma[0].Y.ToString();

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }
        //Gamma自动载入
        private void Multi_LoadGamma(string path)
        {
            string strtmp = "";
            //读取文本信息
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string strLine = streamReader.ReadLine();
            //循环读取文本信息
            while (!string.IsNullOrEmpty(strLine))
            {
                strtmp = strtmp + strLine + ",";
                strLine = streamReader.ReadLine(); //循环读取下一行
            }
            fs.Close(); //关闭流
            streamReader.Close();
            string[] arrtmp = strtmp.Split(new Char[] { ',' });
            RM692Fx_R0.Text = arrtmp[1].ToString(); RM692Fx_G0.Text = arrtmp[3].ToString(); RM692Fx_B0.Text = arrtmp[5].ToString();
            RM692Fx_R1.Text = arrtmp[7].ToString(); RM692Fx_G1.Text = arrtmp[9].ToString(); RM692Fx_B1.Text = arrtmp[11].ToString();
            RM692Fx_R2.Text = arrtmp[13].ToString(); RM692Fx_G2.Text = arrtmp[15].ToString(); RM692Fx_B2.Text = arrtmp[17].ToString();
            RM692Fx_R3.Text = arrtmp[19].ToString(); RM692Fx_G3.Text = arrtmp[21].ToString(); RM692Fx_B3.Text = arrtmp[23].ToString();
            RM692Fx_R4.Text = arrtmp[25].ToString(); RM692Fx_G4.Text = arrtmp[27].ToString(); RM692Fx_B4.Text = arrtmp[29].ToString();
            RM692Fx_R5.Text = arrtmp[31].ToString(); RM692Fx_G5.Text = arrtmp[33].ToString(); RM692Fx_B5.Text = arrtmp[35].ToString();
            RM692Fx_R6.Text = arrtmp[37].ToString(); RM692Fx_G6.Text = arrtmp[39].ToString(); RM692Fx_B6.Text = arrtmp[41].ToString();
            RM692Fx_R7.Text = arrtmp[43].ToString(); RM692Fx_G7.Text = arrtmp[45].ToString(); RM692Fx_B7.Text = arrtmp[47].ToString();
            RM692Fx_R8.Text = arrtmp[49].ToString(); RM692Fx_G8.Text = arrtmp[51].ToString(); RM692Fx_B8.Text = arrtmp[53].ToString();
            RM692Fx_R9.Text = arrtmp[55].ToString(); RM692Fx_G9.Text = arrtmp[57].ToString(); RM692Fx_B9.Text = arrtmp[59].ToString();
            RM692Fx_R10.Text = arrtmp[61].ToString(); RM692Fx_G10.Text = arrtmp[63].ToString(); RM692Fx_B10.Text = arrtmp[65].ToString();
            RM692Fx_R11.Text = arrtmp[67].ToString(); RM692Fx_G11.Text = arrtmp[69].ToString(); RM692Fx_B11.Text = arrtmp[71].ToString();
            RM692Fx_R12.Text = arrtmp[73].ToString(); RM692Fx_G12.Text = arrtmp[75].ToString(); RM692Fx_B12.Text = arrtmp[77].ToString();
            RM692Fx_R13.Text = arrtmp[79].ToString(); RM692Fx_G13.Text = arrtmp[81].ToString(); RM692Fx_B13.Text = arrtmp[83].ToString();
            RM692Fx_R14.Text = arrtmp[85].ToString(); RM692Fx_G14.Text = arrtmp[87].ToString(); RM692Fx_B14.Text = arrtmp[89].ToString();
            RM692Fx_R15.Text = arrtmp[91].ToString(); RM692Fx_G15.Text = arrtmp[93].ToString(); RM692Fx_B15.Text = arrtmp[95].ToString();
            RM692Fx_R16.Text = arrtmp[97].ToString(); RM692Fx_G16.Text = arrtmp[99].ToString(); RM692Fx_B16.Text = arrtmp[101].ToString();
            RM692Fx_R17.Text = arrtmp[103].ToString(); RM692Fx_G17.Text = arrtmp[105].ToString(); RM692Fx_B17.Text = arrtmp[107].ToString();
            RM692Fx_R18.Text = arrtmp[109].ToString(); RM692Fx_G18.Text = arrtmp[111].ToString(); RM692Fx_B18.Text = arrtmp[113].ToString();
            RM692Fx_R19.Text = arrtmp[115].ToString(); RM692Fx_G19.Text = arrtmp[117].ToString(); RM692Fx_B19.Text = arrtmp[119].ToString();
            RM692Fx_R20.Text = arrtmp[121].ToString(); RM692Fx_G20.Text = arrtmp[123].ToString(); RM692Fx_B20.Text = arrtmp[125].ToString();
            RM692Fx_R21.Text = arrtmp[127].ToString(); RM692Fx_G21.Text = arrtmp[129].ToString(); RM692Fx_B21.Text = arrtmp[131].ToString();
            RM692Fx_R22.Text = arrtmp[133].ToString(); RM692Fx_G22.Text = arrtmp[135].ToString(); RM692Fx_B22.Text = arrtmp[137].ToString();
            RM692Fx_R23.Text = arrtmp[139].ToString(); RM692Fx_G23.Text = arrtmp[141].ToString(); RM692Fx_B23.Text = arrtmp[143].ToString();
            RM692Fx_R24.Text = arrtmp[145].ToString(); RM692Fx_G24.Text = arrtmp[147].ToString(); RM692Fx_B24.Text = arrtmp[149].ToString();
            RM692Fx_R25.Text = arrtmp[151].ToString(); RM692Fx_G25.Text = arrtmp[153].ToString(); RM692Fx_B25.Text = arrtmp[155].ToString();
            RM692Fx_R26.Text = arrtmp[157].ToString(); RM692Fx_G26.Text = arrtmp[159].ToString(); RM692Fx_B26.Text = arrtmp[161].ToString();
        }
        private void UDC694_Gamma_Linear_interpolation(int gamma_band)
        {
            int gamma_front = gamma_band + 1;
            string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + gamma_front.ToString() + ".csv";
            Multi_LoadGamma(pathload);
            Application.DoEvents();
            decimal[] gamma_front_red_reg = new decimal[15] { RM692Fx_R0.Value, RM692Fx_R1.Value, RM692Fx_R2.Value, RM692Fx_R3.Value, RM692Fx_R4.Value,
                                                              RM692Fx_R5.Value, RM692Fx_R6.Value, RM692Fx_R7.Value, RM692Fx_R8.Value, RM692Fx_R9.Value,
                                                              RM692Fx_R10.Value, RM692Fx_R11.Value,RM692Fx_R12.Value, RM692Fx_R13.Value, RM692Fx_R14.Value};
            decimal[] gamma_front_green_reg = new decimal[15] { RM692Fx_G0.Value, RM692Fx_G1.Value, RM692Fx_G2.Value, RM692Fx_G3.Value, RM692Fx_G4.Value,
                                                                RM692Fx_G5.Value, RM692Fx_G6.Value, RM692Fx_G7.Value, RM692Fx_G8.Value, RM692Fx_G9.Value,
                                                                RM692Fx_G10.Value, RM692Fx_G11.Value,RM692Fx_G12.Value, RM692Fx_G13.Value, RM692Fx_G14.Value};
            decimal[] gamma_front_blue_reg = new decimal[15] { RM692Fx_B0.Value, RM692Fx_B1.Value, RM692Fx_B2.Value, RM692Fx_B3.Value, RM692Fx_B4.Value,
                                                               RM692Fx_B5.Value, RM692Fx_B6.Value, RM692Fx_B7.Value, RM692Fx_B8.Value, RM692Fx_B9.Value,
                                                               RM692Fx_B10.Value, RM692Fx_B11.Value,RM692Fx_B12.Value, RM692Fx_B13.Value, RM692Fx_B14.Value};

            RM692Fx_R14.Text = ((int)(((double)(gamma_front_red_reg[14]) - (double)(gamma_front_red_reg[13])) / (239 - 207) * (0.8166 * 255 - 207) + (double)(gamma_front_red_reg[13]))).ToString();
            RM692Fx_R13.Text = ((int)(((double)(gamma_front_red_reg[13]) - (double)(gamma_front_red_reg[12])) / (207 - 143) * (0.8166 * 239 - 143) + (double)(gamma_front_red_reg[12]))).ToString();
            RM692Fx_R12.Text = ((int)(((double)(gamma_front_red_reg[13]) - (double)(gamma_front_red_reg[12])) / (207 - 143) * (0.8166 * 207 - 143) + (double)(gamma_front_red_reg[12]))).ToString();
            RM692Fx_R11.Text = ((int)(((double)(gamma_front_red_reg[12]) - (double)(gamma_front_red_reg[11])) / (143 - 111) * (0.8166 * 143 - 111) + (double)(gamma_front_red_reg[11]))).ToString();
            RM692Fx_R10.Text = ((int)(((double)(gamma_front_red_reg[11]) - (double)(gamma_front_red_reg[10])) / (111 - 79) * (0.8166 * 111 - 79) + (double)(gamma_front_red_reg[10]))).ToString();
            RM692Fx_R9.Text = ((int)(((double)(gamma_front_red_reg[10]) - (double)(gamma_front_red_reg[9])) / (79 - 63) * (0.8166 * 79 - 63) + (double)(gamma_front_red_reg[9]))).ToString();
            RM692Fx_R8.Text = ((int)(((double)(gamma_front_red_reg[9]) - (double)(gamma_front_red_reg[8])) / (63 - 47) * (0.8166 * 63 - 47) + (double)(gamma_front_red_reg[8]))).ToString();
            RM692Fx_R7.Text = ((int)(((double)(gamma_front_red_reg[8]) - (double)(gamma_front_red_reg[7])) / (47 - 31) * (0.8166 * 47 - 31) + (double)(gamma_front_red_reg[7]))).ToString();
            RM692Fx_R6.Text = ((int)(((double)(gamma_front_red_reg[7]) - (double)(gamma_front_red_reg[6])) / (31 - 23) * (0.8166 * 31 - 23) + (double)(gamma_front_red_reg[6]))).ToString();
            RM692Fx_R5.Text = ((int)(((double)(gamma_front_red_reg[6]) - (double)(gamma_front_red_reg[5])) / (23 - 15) * (0.8166 * 23 - 15) + (double)(gamma_front_red_reg[5]))).ToString();
            RM692Fx_R4.Text = ((int)(((double)(gamma_front_red_reg[5]) - (double)(gamma_front_red_reg[4])) / (15 - 7) * (0.8166 * 15 - 7) + (double)(gamma_front_red_reg[4]))).ToString();
            RM692Fx_R3.Text = ((int)(((double)(gamma_front_red_reg[4]) - (double)(gamma_front_red_reg[3])) / (7 - 3) * (0.8166 * 7 - 3) + (double)(gamma_front_red_reg[3]))).ToString();
            RM692Fx_R2.Text = ((int)(((double)(gamma_front_red_reg[3]) - (double)(gamma_front_red_reg[2])) / (3 - 1) * (0.8166 * 3 - 1) + (double)(gamma_front_red_reg[2]))).ToString();
            RM692Fx_R1.Text = ((int)(((double)(gamma_front_red_reg[2]) - (double)(gamma_front_red_reg[1])) / (1 - 0) * (0.8166 * 1 - 0) + (double)(gamma_front_red_reg[1]))).ToString();
            RM692Fx_R0.Text = "0";

            RM692Fx_G14.Text = ((int)(((double)(gamma_front_green_reg[14]) - (double)(gamma_front_green_reg[13])) / (239 - 207) * (0.8166 * 255 - 207) + (double)(gamma_front_green_reg[13]))).ToString();
            RM692Fx_G13.Text = ((int)(((double)(gamma_front_green_reg[13]) - (double)(gamma_front_green_reg[12])) / (207 - 143) * (0.8166 * 239 - 143) + (double)(gamma_front_green_reg[12]))).ToString();
            RM692Fx_G12.Text = ((int)(((double)(gamma_front_green_reg[13]) - (double)(gamma_front_green_reg[12])) / (207 - 143) * (0.8166 * 207 - 143) + (double)(gamma_front_green_reg[12]))).ToString();
            RM692Fx_G11.Text = ((int)(((double)(gamma_front_green_reg[12]) - (double)(gamma_front_green_reg[11])) / (143 - 111) * (0.8166 * 143 - 111) + (double)(gamma_front_green_reg[11]))).ToString();
            RM692Fx_G10.Text = ((int)(((double)(gamma_front_green_reg[11]) - (double)(gamma_front_green_reg[10])) / (111 - 79) * (0.8166 * 111 - 79) + (double)(gamma_front_green_reg[10]))).ToString();
            RM692Fx_G9.Text = ((int)(((double)(gamma_front_green_reg[10]) - (double)(gamma_front_green_reg[9])) / (79 - 63) * (0.8166 * 79 - 63) + (double)(gamma_front_green_reg[9]))).ToString();
            RM692Fx_G8.Text = ((int)(((double)(gamma_front_green_reg[9]) - (double)(gamma_front_green_reg[8])) / (63 - 47) * (0.8166 * 63 - 47) + (double)(gamma_front_green_reg[8]))).ToString();
            RM692Fx_G7.Text = ((int)(((double)(gamma_front_green_reg[8]) - (double)(gamma_front_green_reg[7])) / (47 - 31) * (0.8166 * 47 - 31) + (double)(gamma_front_green_reg[7]))).ToString();
            RM692Fx_G6.Text = ((int)(((double)(gamma_front_green_reg[7]) - (double)(gamma_front_green_reg[6])) / (31 - 23) * (0.8166 * 31 - 23) + (double)(gamma_front_green_reg[6]))).ToString();
            RM692Fx_G5.Text = ((int)(((double)(gamma_front_green_reg[6]) - (double)(gamma_front_green_reg[5])) / (23 - 15) * (0.8166 * 23 - 15) + (double)(gamma_front_green_reg[5]))).ToString();
            RM692Fx_G4.Text = ((int)(((double)(gamma_front_green_reg[5]) - (double)(gamma_front_green_reg[4])) / (15 - 7) * (0.8166 * 15 - 7) + (double)(gamma_front_green_reg[4]))).ToString();
            RM692Fx_G3.Text = ((int)(((double)(gamma_front_green_reg[4]) - (double)(gamma_front_green_reg[3])) / (7 - 3) * (0.8166 * 7 - 3) + (double)(gamma_front_green_reg[3]))).ToString();
            RM692Fx_G2.Text = ((int)(((double)(gamma_front_green_reg[3]) - (double)(gamma_front_green_reg[2])) / (3 - 1) * (0.8166 * 3 - 1) + (double)(gamma_front_green_reg[2]))).ToString();
            RM692Fx_G1.Text = ((int)(((double)(gamma_front_green_reg[2]) - (double)(gamma_front_green_reg[1])) / (1 - 0) * (0.8166 * 1 - 0) + (double)(gamma_front_green_reg[1]))).ToString();
            RM692Fx_G0.Text = "0";

            RM692Fx_B14.Text = ((int)(((double)(gamma_front_blue_reg[14]) - (double)(gamma_front_blue_reg[13])) / (239 - 207) * (0.8166 * 255 - 207) + (double)(gamma_front_blue_reg[13]))).ToString();
            RM692Fx_B13.Text = ((int)(((double)(gamma_front_blue_reg[13]) - (double)(gamma_front_blue_reg[12])) / (207 - 143) * (0.8166 * 239 - 143) + (double)(gamma_front_blue_reg[12]))).ToString();
            RM692Fx_B12.Text = ((int)(((double)(gamma_front_blue_reg[13]) - (double)(gamma_front_blue_reg[12])) / (207 - 143) * (0.8166 * 207 - 143) + (double)(gamma_front_blue_reg[12]))).ToString();
            RM692Fx_B11.Text = ((int)(((double)(gamma_front_blue_reg[12]) - (double)(gamma_front_blue_reg[11])) / (143 - 111) * (0.8166 * 143 - 111) + (double)(gamma_front_blue_reg[11]))).ToString();
            RM692Fx_B10.Text = ((int)(((double)(gamma_front_blue_reg[11]) - (double)(gamma_front_blue_reg[10])) / (111 - 79) * (0.8166 * 111 - 79) + (double)(gamma_front_blue_reg[10]))).ToString();
            RM692Fx_B9.Text = ((int)(((double)(gamma_front_blue_reg[10]) - (double)(gamma_front_blue_reg[9])) / (79 - 63) * (0.8166 * 79 - 63) + (double)(gamma_front_blue_reg[9]))).ToString();
            RM692Fx_B8.Text = ((int)(((double)(gamma_front_blue_reg[9]) - (double)(gamma_front_blue_reg[8])) / (63 - 47) * (0.8166 * 63 - 47) + (double)(gamma_front_blue_reg[8]))).ToString();
            RM692Fx_B7.Text = ((int)(((double)(gamma_front_blue_reg[8]) - (double)(gamma_front_blue_reg[7])) / (47 - 31) * (0.8166 * 47 - 31) + (double)(gamma_front_blue_reg[7]))).ToString();
            RM692Fx_B6.Text = ((int)(((double)(gamma_front_blue_reg[7]) - (double)(gamma_front_blue_reg[6])) / (31 - 23) * (0.8166 * 31 - 23) + (double)(gamma_front_blue_reg[6]))).ToString();
            RM692Fx_B5.Text = ((int)(((double)(gamma_front_blue_reg[6]) - (double)(gamma_front_blue_reg[5])) / (23 - 15) * (0.8166 * 23 - 15) + (double)(gamma_front_blue_reg[5]))).ToString();
            RM692Fx_B4.Text = ((int)(((double)(gamma_front_blue_reg[5]) - (double)(gamma_front_blue_reg[4])) / (15 - 7) * (0.8166 * 15 - 7) + (double)(gamma_front_blue_reg[4]))).ToString();
            RM692Fx_B3.Text = ((int)(((double)(gamma_front_blue_reg[4]) - (double)(gamma_front_blue_reg[3])) / (7 - 3) * (0.8166 * 7 - 3) + (double)(gamma_front_blue_reg[3]))).ToString();
            RM692Fx_B2.Text = ((int)(((double)(gamma_front_blue_reg[3]) - (double)(gamma_front_blue_reg[2])) / (3 - 1) * (0.8166 * 3 - 1) + (double)(gamma_front_blue_reg[2]))).ToString();
            RM692Fx_B1.Text = ((int)(((double)(gamma_front_blue_reg[2]) - (double)(gamma_front_blue_reg[1])) / (1 - 0) * (0.8166 * 1 - 0) + (double)(gamma_front_blue_reg[1]))).ToString();
            RM692Fx_B0.Text = "0";
            Application.DoEvents();

            pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + gamma_band.ToString() + ".csv";
            Multi_SaveGamma(pathload);

        }


        private void RM692Fx_AUTO_gamma(Object myObject, EventArgs myEventArgs)
        {
            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                frmHelp.Gamma_auto_adjust_count_set = 180;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Fx gamma调试完毕!\r\n";
                    //---------------------------------add-------------------------------------
                    if (Multi_gamma_txt_next_index < Multi_gamma_txt_line_count)
                    {
                        Multi_gamma_txt_now_index = Multi_gamma_txt_next_index;
                        Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
                    }
                    //---------------------------------add-------------------------------------
                    if (RM692Fx_GIR_State == 1)
                    {
                        RM692Fx_GIR_State = 2;
                        RM692Fx_GIR_FullScreen_TUNING(null, null);
                    }
                }

                if (index < Diven_Gamma[0].Points_num) //绑点切换部分
                {
                    if (cBx_AOD_Auto_Gamma.Checked == true)  //AOD gamma adjust in AOD mode or not
                    {
                        Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
                    }
                    else
                    {
                        frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                        frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                        frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    }
                    Thread.Sleep(50);
                    frmHelp.Msr_CASDK();
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }

            }
            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;
                try
                {
                    switch (index)
                    {
                        case 0: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R14.Value, RM692Fx_G14.Value, RM692Fx_B14.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R13.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G13.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B13.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R14.Value = r;
                                RM692Fx_G14.Value = g;
                                RM692Fx_B14.Value = b;
                                if (RM692Fx_R14.Value <= RM692Fx_R13.Value)
                                {
                                    RM692Fx_R13.Value = r - 5;
                                }
                                if (RM692Fx_G14.Value <= RM692Fx_G13.Value)
                                {
                                    RM692Fx_G13.Value = g - 5;
                                }
                                if (RM692Fx_B14.Value <= RM692Fx_B13.Value)
                                {
                                    RM692Fx_B13.Value = b - 5;
                                }
                            }
                            break;
                        case 1: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R13.Value, RM692Fx_G13.Value, RM692Fx_B13.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R12.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G12.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B12.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R13.Value = r;
                                RM692Fx_G13.Value = g;
                                RM692Fx_B13.Value = b;
                                if (RM692Fx_R13.Value <= RM692Fx_R12.Value)
                                {
                                    RM692Fx_R12.Value = r - 5;
                                }
                                if (RM692Fx_G13.Value <= RM692Fx_G12.Value)
                                {
                                    RM692Fx_G12.Value = g - 5;
                                }
                                if (RM692Fx_B13.Value <= RM692Fx_B12.Value)
                                {
                                    RM692Fx_B12.Value = b - 5;
                                }
                            }
                            break;
                        case 2: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R12.Value, RM692Fx_G12.Value, RM692Fx_B12.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R11.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G11.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B11.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R12.Value = r;
                                RM692Fx_G12.Value = g;
                                RM692Fx_B12.Value = b;
                                if (RM692Fx_R12.Value <= RM692Fx_R11.Value)
                                {
                                    RM692Fx_R11.Value = r - 5;
                                }
                                if (RM692Fx_G12.Value <= RM692Fx_G11.Value)
                                {
                                    RM692Fx_G11.Value = g - 5;
                                }
                                if (RM692Fx_B12.Value <= RM692Fx_B11.Value)
                                {
                                    RM692Fx_B11.Value = b - 5;
                                }
                            }
                            break;
                        case 3: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R11.Value, RM692Fx_G11.Value, RM692Fx_B11.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R10.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G10.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B10.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R11.Value = r;
                                RM692Fx_G11.Value = g;
                                RM692Fx_B11.Value = b;
                                if (RM692Fx_R11.Value <= RM692Fx_R10.Value)
                                {
                                    RM692Fx_R10.Value = r - 5;
                                }
                                if (RM692Fx_G11.Value <= RM692Fx_G10.Value)
                                {
                                    RM692Fx_G10.Value = g - 5;
                                }
                                if (RM692Fx_B11.Value <= RM692Fx_B10.Value)
                                {
                                    RM692Fx_B10.Value = b - 5;
                                }
                            }
                            break;
                        case 4: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R10.Value, RM692Fx_G10.Value, RM692Fx_B10.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R9.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G9.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B9.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R10.Value = r;
                                RM692Fx_G10.Value = g;
                                RM692Fx_B10.Value = b;
                                if (RM692Fx_R10.Value <= RM692Fx_R9.Value)
                                {
                                    RM692Fx_R9.Value = r - 5;
                                }
                                if (RM692Fx_G10.Value <= RM692Fx_G9.Value)
                                {
                                    RM692Fx_G9.Value = g - 5;
                                }
                                if (RM692Fx_B10.Value <= RM692Fx_B9.Value)
                                {
                                    RM692Fx_B9.Value = b - 5;
                                }
                            }
                            break;
                        case 5: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R9.Value, RM692Fx_G9.Value, RM692Fx_B9.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R8.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G8.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B8.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R9.Value = r;
                                RM692Fx_G9.Value = g;
                                RM692Fx_B9.Value = b;
                                if (RM692Fx_R9.Value <= RM692Fx_R8.Value)
                                {
                                    RM692Fx_R8.Value = r - 5;
                                }
                                if (RM692Fx_G9.Value <= RM692Fx_G8.Value)
                                {
                                    RM692Fx_G8.Value = g - 5;
                                }
                                if (RM692Fx_B9.Value <= RM692Fx_B8.Value)
                                {
                                    RM692Fx_B8.Value = b - 5;
                                }
                            }
                            break;
                        case 6: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R8.Value, RM692Fx_G8.Value, RM692Fx_B8.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R7.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G7.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B7.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R8.Value = r;
                                RM692Fx_G8.Value = g;
                                RM692Fx_B8.Value = b;
                                if (RM692Fx_R8.Value <= RM692Fx_R7.Value)
                                {
                                    RM692Fx_R7.Value = r - 5;
                                }
                                if (RM692Fx_G8.Value <= RM692Fx_G7.Value)
                                {
                                    RM692Fx_G7.Value = g - 5;
                                }
                                if (RM692Fx_B8.Value <= RM692Fx_B7.Value)
                                {
                                    RM692Fx_B7.Value = b - 5;
                                }
                            }
                            break;
                        case 7: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R7.Value, RM692Fx_G7.Value, RM692Fx_B7.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R6.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G6.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B6.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R7.Value = r;
                                RM692Fx_G7.Value = g;
                                RM692Fx_B7.Value = b;
                                if (RM692Fx_R7.Value <= RM692Fx_R6.Value)
                                {
                                    RM692Fx_R6.Value = r - 5;
                                }
                                if (RM692Fx_G7.Value <= RM692Fx_G6.Value)
                                {
                                    RM692Fx_G6.Value = g - 5;
                                }
                                if (RM692Fx_B7.Value <= RM692Fx_B6.Value)
                                {
                                    RM692Fx_B6.Value = b - 5;
                                }
                            }
                            break;
                        case 8: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R6.Value, RM692Fx_G6.Value, RM692Fx_B6.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R5.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G5.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B5.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R6.Value = r;
                                RM692Fx_G6.Value = g;
                                RM692Fx_B6.Value = b;
                                if (RM692Fx_R6.Value <= RM692Fx_R5.Value)
                                {
                                    RM692Fx_R5.Value = r - 5;
                                }
                                if (RM692Fx_G6.Value <= RM692Fx_G5.Value)
                                {
                                    RM692Fx_G5.Value = g - 5;
                                }
                                if (RM692Fx_B6.Value <= RM692Fx_B5.Value)
                                {
                                    RM692Fx_B5.Value = b - 5;
                                }

                            }
                            break;
                        case 9: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R5.Value, RM692Fx_G5.Value, RM692Fx_B5.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R4.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G4.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B4.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R5.Value = r;
                                RM692Fx_G5.Value = g;
                                RM692Fx_B5.Value = b;
                                if (RM692Fx_R5.Value <= RM692Fx_R4.Value)
                                {
                                    RM692Fx_R4.Value = r - 5;
                                }
                                if (RM692Fx_G5.Value <= RM692Fx_G4.Value)
                                {
                                    RM692Fx_G4.Value = g - 5;
                                }
                                if (RM692Fx_B5.Value <= RM692Fx_B4.Value)
                                {
                                    RM692Fx_B4.Value = b - 5;
                                }
                                //  RM692Fx_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692Fx_R5.Value), gamma_g[index], Convert.ToUInt16(RM692Fx_G5.Value), gamma_b[index], Convert.ToUInt16(RM692Fx_B5.Value));
                            }
                            break;
                        case 10: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R4.Value, RM692Fx_G4.Value, RM692Fx_B4.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R3.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G3.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B3.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R4.Value = r;
                                RM692Fx_G4.Value = g;
                                RM692Fx_B4.Value = b;
                                if (RM692Fx_R4.Value <= RM692Fx_R3.Value)
                                {
                                    RM692Fx_R3.Value = r - 5;
                                }
                                if (RM692Fx_G4.Value <= RM692Fx_G3.Value)
                                {
                                    RM692Fx_G3.Value = g - 5;
                                }
                                if (RM692Fx_B4.Value <= RM692Fx_B3.Value)
                                {
                                    RM692Fx_B3.Value = b - 5;
                                }
                                //  RM692Fx_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692Fx_R4.Value), gamma_g[index], Convert.ToUInt16(RM692Fx_G4.Value), gamma_b[index], Convert.ToUInt16(RM692Fx_B4.Value));
                            }
                            break;
                        case 11: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R3.Value, RM692Fx_G3.Value, RM692Fx_B3.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R2.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G2.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B2.Value));
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_R3.Value = r;
                                RM692Fx_G3.Value = g;
                                RM692Fx_B3.Value = b;
                                // RM692Fx_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692Fx_R3.Value), gamma_g[index], Convert.ToUInt16(RM692Fx_G3.Value), gamma_b[index], Convert.ToUInt16(RM692Fx_B3.Value));
                            }
                            if (RM692Fx_R3.Value <= RM692Fx_R2.Value)
                            {
                                RM692Fx_R2.Value = r - 1;
                            }
                            if (RM692Fx_G3.Value <= RM692Fx_G2.Value)
                            {
                                RM692Fx_G2.Value = g - 1;
                            }
                            if (RM692Fx_B3.Value <= RM692Fx_B2.Value)
                            {
                                RM692Fx_B2.Value = b - 1;
                            }
                            break;
                        case 12: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R2.Value, RM692Fx_G2.Value, RM692Fx_B2.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R1.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G1.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B1.Value));
                                }
                                RM692Fx_R2.Value = r;
                                RM692Fx_G2.Value = g;
                                RM692Fx_B2.Value = b;
                                if (RM692Fx_R2.Value <= RM692Fx_R1.Value)
                                {
                                    RM692Fx_R1.Value = r - 1;
                                }
                                if (RM692Fx_G2.Value <= RM692Fx_G1.Value)
                                {
                                    RM692Fx_G1.Value = g - 1;
                                }
                                if (RM692Fx_B2.Value <= RM692Fx_B1.Value)
                                {
                                    RM692Fx_B1.Value = b - 1;
                                }

                                //  RM692Fx_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692Fx_R2.Value), gamma_g[index], Convert.ToUInt16(RM692Fx_G2.Value), gamma_b[index], Convert.ToUInt16(RM692Fx_B2.Value));
                            }
                            break;
                        case 13: //9
                            {
                                Gamma_adjust_timeout_flag = true;
                                //  frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_R1.Value, RM692Fx_G1.Value, RM692Fx_B1.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                    RM692Fx_auto_gamma_write((UInt16)index, Convert.ToUInt16(RM692Fx_R1.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_G1.Value), (UInt16)index, Convert.ToUInt16(RM692Fx_B1.Value));
                                }
                                RM692Fx_R1.Value = (int)RM692Fx_R2.Value / 2;
                                RM692Fx_G1.Value = (int)RM692Fx_G2.Value / 2;
                                RM692Fx_B1.Value = (int)RM692Fx_B2.Value / 2;
                                //  RM692Fx_auto_gamma_write(gamma_r[index], Convert.ToUInt16(RM692Fx_R1.Value), gamma_g[index], Convert.ToUInt16(RM692Fx_G1.Value), gamma_b[index], Convert.ToUInt16(RM692Fx_B1.Value));
                            }
                            break;
                        case 14: //
                            {
                                Gamma_adjust_timeout_flag = true;
                                RM692Fx_R0.Value = 0;
                                RM692Fx_G0.Value = 0;
                                RM692Fx_B0.Value = 0;
                                Application.DoEvents();
                            }
                            break;
                        default: break;
                    }
                    RM692Fx_auto_gamma_write((UInt16)index, (ushort)(r), gamma_g[index], (ushort)(g), gamma_b[index], (ushort)(b));
                    // RM692Fx_auto_gamma_write(gamma_r[14 - index], (ushort)(r), gamma_g[14 - index], (ushort)(g), gamma_b[14 - index], (ushort)(b));
                    Thread.Sleep(50);

                }
                catch
                {
                    //   T20.Enabled = false;
                    //    MessageBox.Show("数值超限！");
                }
            }
        }
        private void Msr_CA310()
        {
            frmHelp.Msr_CASDK();
            auto_lum = frmHelp.CASDK_LUM;
            auto_x = frmHelp.CASDK_x;
            auto_y = frmHelp.CASDK_y;
        }   //获取CA310/CA410读数
        bool JumpGammaAdjustFlag = false;  //跳过某个绑点标志位，通常用于末尾低灰阶
        UInt32 timeout = 0;
        private void T17_Tick(object sender, EventArgs e)   //AutoGamma处理过程
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            T17.Enabled = false;
            T17.Interval = 5;  //不能过短，要给显示画面一个稳定的时间

            //if (!ReceivOKFlag)
            //{
            //    numPatternR.Value = RM692EX_Point[26 - index];
            //    numPatternG.Value = RM692EX_Point[26 - index];
            //    numPatternB.Value = RM692EX_Point[26 - index];
            //    OLED_Show_Click(btnPatternW, e);//写入Pattern参数
            //}
            Msr_CA310();
            txtlvshow.Text = "Lv: " + auto_lum.ToString("f4") + "\r\n" + "x: " + auto_x.ToString("f4") + "\r\n" + "y: " + auto_y.ToString("f4") + "\r\n";
            labTargetlum.Text = "Tar:" + D_lum[GPN - index].ToString("#0.00000");  // 在UI上显示当前绑点的目标亮度值
            frmHelp.textBox1.Text += "Adjust inex:" + GPN + "----" + index + "\r\n";

            lum_off = (D_lum[GPN - index] * Convert.ToDouble(numVT_loff.Value) / 1000);  //计算当前绑点允许的亮度误差范围
            x_off = Convert.ToDouble(txtVT_xoff.Text);
            y_off = Convert.ToDouble(txtVT_yoff.Text);       //获取色坐标允许误差范围

            if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))//
                flag1 = 0;  //判断亮度以及色坐标是否在目标范围
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                flag2 = 0;  //判断亮度以及色坐标是否在目标范围
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                flag3 = 0;  //判断亮度以及色坐标是否在目标范围
            if ((flag1 == 0 && flag2 == 0 && flag3 == 0) && (index < 10)) //低灰阶本来就很难调就不这么折腾了
            {   //初次判断调整OK后，再来复测一次，以消除偶发调不准问题
                Msr_CA310();
                if ((auto_lum < (D_lum[GPN - index] + lum_off)) && (auto_lum > (D_lum[GPN - index] - lum_off)))//
                    flag1 = 0;  //判断亮度以及色坐标是否在目标范围
                else flag1 = 1;
                if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                    flag2 = 0;  //判断亮度以及色坐标是否在目标范围
                else flag2 = 1;
                if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                    flag3 = 0;  //判断亮度以及色坐标是否在目标范围
                else flag3 = 1;
            }//初次判断调整OK后，再来复测一次，以消除偶发调不准问题,

            Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
            Lb_t = 1.0;
            Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
            lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
            lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
            lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

            if (((flag1 == 0 && flag2 == 0 && flag3 == 0) || JumpGammaAdjustFlag) || (timeout > 3 * numTurnGammaTimeout.Value))
            {
                if (timeout > 3 * numTurnGammaTimeout.Value) { frmHelp.textBox1.Text += "--模糊处理--\r\n" + "----模糊处理----\r\n" + "--------模糊处理--------\r\n" + "----模糊处理----\r\n" + "--模糊处理--\r\n"; }
                timeout = 0;
                index++;
                if (index == GPN)   //  最后一个绑点通常为0，不用调试结束即可
                {
                    //TimTest.Stop();//停止计时
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692EX gamma调试完毕!\r\n";
                    //txttip.Focus();//获取焦点
                    //txttip.Select(txttip.TextLength, 0);//光标定位到文本最后
                    //txttip.ScrollToCaret();//滚动到光标处
                    STOPAutoGammaFlag = true;
                    T17.Enabled = false;
                    //btn_RM692EX_Stop.PerformClick();
                    return;
                }
                if (index < GPN)
                {
                    //frmHelp.textBox1.Text += index + "完成...\r\n";
                    //txttip.Select(txttip.Text.Length, 0);
                    //txttip.ScrollToCaret();    //上滚提示信息以便于查看
                    for (int i = 0; i < 25; i++)    //本意用于显示当前绑点目标信息，但一般用不到
                    {
                        //Convert.ToString(要转的数, 16);//string s = string.Format("0x{0:X2}", i);    // s = 0x0F;
                        //Points_LV.Text += (i + 1) + ",R/G/B, V" + RM692EX_Point[24 - i] + ": " + string.Format("0x{0:X2}", GammaValue[24 - i, 0]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 1]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 2]) + "\r\n";

                        //this.GammaReg_dataGridView.Rows.Clear();
                        //DataGirdShow((byte)i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                        //Points_LV.Text += (i + 1) + ",R/G/B, V" + RM692EX_Point[24 - i] + ": " + string.Format("0x{0:X2}", GammaValue[24 - i, 0]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 1]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 2]) + "\r\n";
                    }
                    //Thread.Sleep(10); //1秒=1000毫秒，
                    //T17.Enabled = true;
                    flag1 = 1;  //复位 亮度色坐标符合目标标志位
                    flag2 = 1;
                    flag3 = 1;
                    JumpGammaAdjustFlag = false;
                }
                rstep = (byte)(int.Parse(numStep.Value.ToString())); ;    //重新获取调节步长
                gstep = (byte)(int.Parse(numStep.Value.ToString())); ;
                bstep = (byte)(int.Parse(numStep.Value.ToString())); ;
                frmHelp.numPatternR.Value = RM692EX_Point[GPN - index];     //设定绑点灰阶
                                                                            //numPatternG.Value = RM692EX_Point[GPN - index];
                                                                            //numPatternB.Value = RM692EX_Point[GPN - index];
                                                                            //if (chkTurnsubGamma.Checked)
                                                                            //{
                                                                            //    frmHelp.btnPrevPic_Click(null, null);
                                                                            //}
                                                                            //else
                frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);//写入Pattern参数   ，显示帮点对应灰阶画面
                //ReceivOKFlag = false;
                Thread.Sleep(10);
                Msr_CA310();
                Msr_CA310(); //修复讨厌的灰阶跳变时CA410数据异常的问题
                if (!STOPAutoGammaFlag)
                    T17.Enabled = true;
            }
            else
            {
                if ((GPN - index == 1) | (GPN - index == 2))//这里index最大值为绑点数量-1；倒数第2个绑点 以及倒数第3个绑点可以做技术处理
                {
                    //GammaValue[0, 1] = 0;  //0绑点不调，直接送零
                    //GammaValue[0, 2] = 0;
                    //GammaValue[0, 0] = 0;
                    GammaValue[GPN - index, 0] = (GammaValue[GPN - index + 1, 0] >> 1) - (GammaValue[GPN - index + 1, 0] >> 2);  //绑点不调，直接赋值算了，如果有过度不均再手动修正即可
                    GammaValue[GPN - index, 1] = (GammaValue[GPN - index + 1, 1] >> 1) - (GammaValue[GPN - index + 1, 1] >> 2);
                    GammaValue[GPN - index, 2] = (GammaValue[GPN - index + 1, 2] >> 1) - (GammaValue[GPN - index + 1, 2] >> 2);
                    JumpGammaAdjustFlag = true;
                }
                //else if (GPN - index == 2) //倒数第2个绑点
                //{
                //}
                else
                {
                    if ((flag3 == 1) | (flag2 == 1) | (flag1 == 1))   //这个判断的意义可以猜猜看
                    {
                        {   //Gamma --R----
                            if (lumr > D_lums[GPN - index, 0])
                            {
                                if ((!lastdirupR)) { timeout++; }
                                if ((!lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] - rstep;// (byte)(int.Parse(numStep.Value.ToString()));
                                lastdirupR = true;
                            }
                            else
                            {
                                if ((lastdirupR)) { timeout++; }
                                if ((lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                GammaValue[GPN - index, 0] = GammaValue[GPN - index, 0] + rstep;
                                lastdirupR = false;
                            }
                        }
                        {//Gamma ---G--
                            if (lumg > D_lums[GPN - index, 1])
                            {
                                if ((!lastdirupG)) { timeout++; }
                                if ((!lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] - gstep;
                                lastdirupG = true;
                            }
                            else
                            {
                                if ((lastdirupG)) { timeout++; }
                                if ((lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                GammaValue[GPN - index, 1] = GammaValue[GPN - index, 1] + gstep;
                                lastdirupG = false;
                            }
                        }
                        {//--Gamma  ---B---
                            if (lumb > D_lums[GPN - index, 2])
                            {
                                if ((!lastdirupB)) { timeout++; }
                                if ((!lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] - bstep;
                                lastdirupB = true;
                            }
                            else
                            {
                                if ((lastdirupB)) { timeout++; }
                                if ((lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                GammaValue[GPN - index, 2] = GammaValue[GPN - index, 2] + bstep;
                                lastdirupB = false;
                            }
                        }
                        frmHelp.textBox1.Text += "lumR:" + lumr + ";D_lums[GPN - index, 0]:" + D_lums[GPN - index, 0] + "\r\n";
                        frmHelp.textBox1.Text += "lumG:" + lumg + ";D_lums[GPN - index, 1]:" + D_lums[GPN - index, 1] + "\r\n";
                        frmHelp.textBox1.Text += "lumB:" + lumb + ";D_lums[GPN - index, 2]:" + D_lums[GPN - index, 2] + "\r\n";
                        frmHelp.textBox1.Text += "rstep:" + rstep + ";gstetp:" + gstep + ";bstep:" + bstep + "\r\n";
                        //txttip.Select(txttip.Text.Length, 0);
                        //txttip.ScrollToCaret();
                    }   //如果Gamma Value不在目标范围内，此处修正之
                    CheckandCorrectGammaValue();
                    GammaValueFormat_Click(null, null);
                    //显示当前(待)写入的Gamma寄存器值
                    frmHelp.textBox1.Text += "R:" + GammaValue[GPN - index, 0] + "\r\n" + ";G:" + GammaValue[GPN - index, 1] + "\r\n" + ";B:" + GammaValue[GPN - index, 2] + "\r\n";
                    frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                    frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                    GammaValueSet(1, 1, 1, 1, 1, 1, index, GammaValueTeparr);
                    Thread.Sleep(20);
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死                            
                }
                if (!STOPAutoGammaFlag)
                    T17.Enabled = true;
            }
        }

        private void RM692Fx_AUTO_LUM(Object myObject, EventArgs myEventArgs)
        {//大白纯亮度
            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
            }

            if (flag1 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                frmHelp.Gamma_auto_adjust_count_set = 100;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Fx 大白亮度调试完毕!\r\n";
                    //---------------------------------add-------------------------------------
                    RM692Fx_GIR_State = 3;
                    RM692Fx_GIR_FullScreen_TUNING(null, null);
                    //---------------------------------add-------------------------------------
                }

                if (index < Diven_Gamma[0].Points_num) //绑点切换部分
                {
                    frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                    frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    Thread.Sleep(50);
                    frmHelp.Msr_CASDK();
                    T20.Enabled = true;
                    flag1 = 1;
                }
            }
            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;
                try
                {
                    switch (index)
                    {
                        case 0: //9
                            {
                                decimal RM692Fx_HFZ = RM692Fx_GIR_26H.Value * 255 + RM692Fx_GIR_27H.Value;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, 0, 0, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], 0, RM692Fx_HFZ, 0, Diven_Offset[index].NO, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0;
                                }
                                RM692Fx_HFZ = g;
                                RM692Fx_GIR_26H.Value = (int)(g) >> 8;
                                RM692Fx_GIR_27H.Value = (int)(g) & 0x0ff;
                            }
                            break;
                        default: break;
                    }
                    RM692Fx_Others_Click(btn_RM692Fx_GIR_SET, null);
                    Thread.Sleep(50);
                }
                catch
                {
                    //    T20.Enabled = false;
                    //    MessageBox.Show("数值超限！");
                }
            }
        }

        private void RM692Fx_AUTO_GIR(Object myObject, EventArgs myEventArgs)
        {
            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                frmHelp.Gamma_auto_adjust_count_set = 70;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM692Fx 大白调试完毕!\r\n";
                    //---------------------------------add-------------------------------------
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "大小白亮度Check！\r\n";
                    RM692Fx_GIR_CHECK(null, null);
                    //---------------------------------add-------------------------------------
                }

                if (index < Diven_Gamma[0].Points_num) //绑点切换部分
                {
                    if (cBx_AOD_Auto_Gamma.Checked == true)  //AOD gamma adjust in AOD mode or not
                    {
                        Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
                    }
                    else
                    {
                        frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                        frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                        frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    }
                    Thread.Sleep(50);
                    frmHelp.Msr_CASDK();
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }

            }
            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;
                try
                {
                    switch (index)
                    {
                        case 0: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692Fx_GIR_R.Value, RM692Fx_GIR_G.Value, RM692Fx_GIR_B.Value, Diven_Offset[index].NO, 0xff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692Fx_GIR_R.Value = r;
                                RM692Fx_GIR_G.Value = g;
                                RM692Fx_GIR_B.Value = b;
                            }
                            break;
                        default: break;
                    }
                    RM692Fx_Others_Click(btn_RM692Fx_GIR_SET, null);
                    Thread.Sleep(50);
                }
                catch
                {
                    //    T20.Enabled = false;
                    //    MessageBox.Show("数值超限！");
                }
            }
        }



        private void RM692Fx_auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;

            Send_buf[0] = 0x68;
            //if (radioButton_Page50.Checked == true)
            //{
            //    Send_buf[1] = 0x05; //register normal gamma
            //}
            //else
            //{
            Send_buf[1] = 0xDD; //register 
            //}
            Send_buf[2] = 8; //length
            Send_buf[3] = 1;

            Send_buf[4] = (byte)(addr & 0xFF);      //发送哪个绑点  0代表255绑点.
            Send_buf[5] = Convert.ToByte(datar >> 8);       //r
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(datag >> 8);      //g
            Send_buf[8] = Convert.ToByte(datag & 0xff);
            Send_buf[9] = Convert.ToByte(Datab >> 8);      //b
            Send_buf[10] = Convert.ToByte(Datab & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[11], out Send_buf[12]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 13);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }
        private void RM692Fx_trackBar_Scroll(object sender, EventArgs e)
        {
            int bits_51h = Convert.ToInt16(Math.Pow(2, Convert.ToByte(RM692Fx_51h_Bits.Text))) - 1;
            label_RM692Fx_trackBar.Text = bits_51h + "阶";
            RM692Fx_trackBar.Maximum = bits_51h;

            RM692Fx_Reg_Data.Text = "fe,00";
            RM692Fx_Others_Click(RM692Fx_RegW, e);
            if (bits_51h < 0x100)
            {
                RM692Fx_Reg_Data.Text = "51," + RM692Fx_trackBar.Value.ToString("x2");
            }
            else
            {
                RM692Fx_Reg_Data.Text = "51" + "," + RM692Fx_trackBar.Value.ToString("x4").Substring(0, 2) + "," + RM692Fx_trackBar.Value.ToString("x4").Substring(2, 2);
            }
            RM692Fx_Others_Click(RM692Fx_RegW, e);
        }
        private void T20_Tick(object sender, EventArgs e)
        {
            if (RM692Fx_GIR_State == 2)
            {
                RM692Fx_AUTO_LUM(null, null);
            }
            else if (RM692Fx_GIR_State == 3)
            {
                RM692Fx_AUTO_GIR(null, null);
            }
            else if (RM692Fx_GIR_State == 1)
            {
                RM692Fx_btnlogic_go_Click(null, null);//显示小白
                RM692Fx_AUTO_gamma(null, null);
            }
            else
            {
                RM692Fx_AUTO_gamma(null, null);
            }
        }
        private void Btn_load_gamma_patten_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            Gamma_loading_path.Text = fldb.SelectedPath;

            if (!Directory.Exists(Gamma_loading_path.Text))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
        }
        public bool start_15gamma = true;//667项目15绑点模式  默认开启15绑点模式
        private void button_382gamma_Click(object sender, EventArgs e)
        {
            if (start_15gamma == false)
            {
                RM692Fx_R15.Enabled = RM692Fx_R16.Enabled = RM692Fx_R17.Enabled = RM692Fx_R18.Enabled = RM692Fx_R19.Enabled =
                RM692Fx_R20.Enabled = RM692Fx_R21.Enabled = RM692Fx_R22.Enabled = RM692Fx_R23.Enabled = RM692Fx_R24.Enabled =
                RM692Fx_R25.Enabled = RM692Fx_R26.Enabled = false;
                RM692Fx_G15.Enabled = RM692Fx_G16.Enabled = RM692Fx_G17.Enabled = RM692Fx_G18.Enabled = RM692Fx_G19.Enabled =
                RM692Fx_G20.Enabled = RM692Fx_G21.Enabled = RM692Fx_G22.Enabled = RM692Fx_G23.Enabled = RM692Fx_G24.Enabled =
                RM692Fx_G25.Enabled = RM692Fx_G26.Enabled = false;
                RM692Fx_B15.Enabled = RM692Fx_B16.Enabled = RM692Fx_B17.Enabled = RM692Fx_B18.Enabled = RM692Fx_B19.Enabled =
                RM692Fx_B20.Enabled = RM692Fx_B21.Enabled = RM692Fx_B22.Enabled = RM692Fx_B23.Enabled = RM692Fx_B24.Enabled =
                RM692Fx_B25.Enabled = RM692Fx_B26.Enabled = false;

            }
            else
            {
                RM692Fx_R15.Enabled = RM692Fx_R16.Enabled = RM692Fx_R17.Enabled = RM692Fx_R18.Enabled = RM692Fx_R19.Enabled =
                RM692Fx_R20.Enabled = RM692Fx_R21.Enabled = RM692Fx_R22.Enabled = RM692Fx_R23.Enabled = RM692Fx_R24.Enabled =
                RM692Fx_R25.Enabled = RM692Fx_R26.Enabled = true;
                RM692Fx_G15.Enabled = RM692Fx_G16.Enabled = RM692Fx_G17.Enabled = RM692Fx_G18.Enabled = RM692Fx_G19.Enabled =
                RM692Fx_G20.Enabled = RM692Fx_G21.Enabled = RM692Fx_G22.Enabled = RM692Fx_G23.Enabled = RM692Fx_G24.Enabled =
                RM692Fx_G25.Enabled = RM692Fx_G26.Enabled = true;
                RM692Fx_R15.Enabled = RM692Fx_R16.Enabled = RM692Fx_B17.Enabled = RM692Fx_B18.Enabled = RM692Fx_B19.Enabled =
                RM692Fx_B20.Enabled = RM692Fx_B21.Enabled = RM692Fx_B22.Enabled = RM692Fx_B23.Enabled = RM692Fx_B24.Enabled =
                RM692Fx_B25.Enabled = RM692Fx_B26.Enabled = true;
            }
        }
        private void Page_switch(object sender, EventArgs e)
        {
            if (radioButton_Page50.Checked == true)
            { radioButton_Page52.Checked = false; }

            if (radioButton_Page52.Checked == true)
            { radioButton_Page50.Checked = false; }
        }
        private void RM692Fx_AUTO_gamma_stop(object sender, EventArgs e)
        {
            T20.Enabled = false;
            T17.Enabled = false;
            STOPAutoGammaFlag = true;
        }
        private void RM692Fx_Inter_Gamma_Click(object sender, EventArgs e)
        {
            RM692Fx_Reg_Data.Text = "FE,70";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "71,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "72,31";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "FE,40";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "83,00";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "FE,50";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);

            //RM692Fx_Reg_Data.Text = "FE,70";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "28,00"; //Disable Gamma int
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            ////----------------------------------------------
            //RM692Fx_Reg_Data.Text = "FE,40";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "DA,C8";    /////Enable Measure mode  //15 point
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "D7," + int.Parse(Gamma_Band_Select.Text).ToString("x2");
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            ////----------------------------------------------
            //RM692Fx_Reg_Data.Text = "FE,78";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "F4,C0"; 
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //if (rBt_RM692Fx_Normal.Checked == true)
            //{
            //    RM692Fx_Reg_Data.Text = "F5," + int.Parse(Gamma_Band_Select.Text).ToString("x2");
            //    RM692Fx_Others_Click(RM692Fx_RegW, null);
            //}
            //else if (rBt_RM692Fx_UDC.Checked == true)
            //{
            //    RM692Fx_Reg_Data.Text = "F6," + int.Parse(Gamma_Band_Select.Text).ToString("x2");
            //    RM692Fx_Others_Click(RM692Fx_RegW, null);
            //}//主屏在F5,副屏在F6
        }
        private void RM692Fx_Exit_Gamma_Click(object sender, EventArgs e)
        {
            //RM692Fx_Reg_Data.Text = "FE,40";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "E3,41";////GAMMA1  // [e3]-45 //gamma5
            //                                //[e3]-49 //gamma9
            //                                //[e6]-14 //gamma20
            //                                //[e3]-40 //gamma20
            //                                //[e6]-15 //gamma21
            //                                //[e3]-40 //gamma21
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);


            RM692Fx_Reg_Data.Text = "FE,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "28,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            Thread.Sleep(1000);
            RM692Fx_Reg_Data.Text = "FE,84";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "DF,93";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);

            RM692Fx_Reg_Data.Text = "FE,70";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "71,01";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            Thread.Sleep(300);/////DELAY30ms
            RM692Fx_Reg_Data.Text = "72,30";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);

            RM692Fx_Reg_Data.Text = "FE,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "51,0F,FF";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "FE,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "29,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "fe,40";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "DA,88";  ///Disalbe Measure mode //15  point
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "FE,78";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "F4,80";    //////Disable Gamma measure
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "FE,70";    
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "28,01";    /////enable Gamma int
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
        }

        private void Btn_RM692Fx_GIR_Load_Click(object sender, EventArgs e)
        {
            string temp0 = frmHelp.Generate_textBox.Text;
            frmHelp.Generate_textBox.Text = txtCmd_GIR.Text;
            frmHelp.Generate_File_Click(frmHelp.Generate_File_Run, null);
            frmHelp.Generate_textBox.Text = temp0;
        }
        private void RM692Fx_btnlogic_go_Click(object sender, EventArgs e)
        {
            /// <param name="LOGIC_TYPE"></图类型>
            /// <param name="M_R"></底色-红通道>
            /// <param name="M_G"></底色-绿通道>
            /// <param name="M_B"></底色-蓝通道>
            /// <param name="L_R"></局部-色红通道>
            /// <param name="L_G"></局部-色红通道>
            /// <param name="L_B"></局部-色红通道>
            /// <param name="X0"></局部-水平方向起点坐标>
            /// <param name="X1"></局部-水平方向终点坐标>
            /// <param name="Y0"></局部-竖直方向终点坐标>
            /// <param name="Y1"></局部-竖直方向终点坐标>
            /// <param name="R0"></局部-半径>
            frmHelp.OLED_LOGIC_G0(0, 0, 0, 0, 255, 255, 255, (ushort)(X0.Value), (ushort)(X1.Value), (ushort)(Y0.Value), (ushort)(Y1.Value), 0);
        }

        private int RM692Fx_GIR_State = 0;
        private void RM692Fx_GIR_TUNING_Click(object sender, EventArgs e)
        {
            double L_X, L_Y, L_Z;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            if (RM692Fx_GIR_State == 0)
            {
                Btn_RM692Fx_GIR_Load_Click(null, null);//载入GIR CODE
                Thread.Sleep(150);
            }
            RM692Fx_GIR_State = 1;
            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            gamma_r = GAMMAs.RM692EX_Gamma_r;
            gamma_g = GAMMAs.RM692EX_Gamma_g;
            gamma_b = GAMMAs.RM692EX_Gamma_b;
            btn_RM692Fx_Calculate_Click(null, null);
            RM692Fx_btnlogic_go_Click(null, null);//显示小白
            Thread.Sleep(50);

            T20.Enabled = true;
        }
        private void RM692Fx_GIR_CHECK(object sender, EventArgs e)
        {
            double lum_fullscreen, lum_local, soul_lum;
            RM692Fx_btnlogic_go_Click(null, null);//显示小白
            Thread.Sleep(150);
            frmHelp.Msr_CASDK();
            lum_local = frmHelp.CASDK_LUM;
            frmHelp.OLED_Show_Click(frmHelp.btnwhite, null); //显示大白
            Thread.Sleep(150);
            frmHelp.Msr_CASDK(); lum_fullscreen = frmHelp.CASDK_LUM;
            soul_lum = Math.Abs(lum_local - lum_fullscreen);
            if ((double)(RM692Fx_GIR_Spec.Value) < soul_lum) //CHECK 大小白亮度差
            {
                RM692Fx_GIR_State = 1;
                RM692Fx_GIR_TUNING_Click(null, null);//重新开始
            }
            else
            {
                RM692Fx_GIR_State = 0;
                MessageBox.Show("GIR调整完成！");
            }
        }
        private void RM692Fx_GIR_FullScreen_TUNING(object sender, EventArgs e)
        {
            double L_X, L_Y, L_Z;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            gamma_r = GAMMAs.RM692EX_Gamma_r;
            gamma_g = GAMMAs.RM692EX_Gamma_g;
            gamma_b = GAMMAs.RM692EX_Gamma_b;
            btn_RM692Fx_Calculate_Click(null, null);
            frmHelp.OLED_Show_Click(frmHelp.btnwhite, e); //显示大白
            Thread.Sleep(50);
            T20.Enabled = true;
        }

        private void RM692Fx_GIR_RESET_Click(object sender, EventArgs e)
        {
            RM692Fx_GIR_26H.Value = 1;
            RM692Fx_GIR_27H.Value = 220;
            RM692Fx_GIR_R.Value = 180;
            RM692Fx_GIR_G.Value = 180;
            RM692Fx_GIR_B.Value = 180;
            RM692Fx_GIR_State = 0;
        }

        private void btnLoadGamma_Click(object sender, EventArgs e)
        {
            string tepGammaR = ""; string tepGammaG = ""; string tepGammaB = "";
            string tepstr = "";
            string vtdrGammatxt = "";
            Int16 gdattmp;
            {
                //Button btnTmp = (Button)sender;
                //switch (btnTmp.Name)
                {
                    //    case "btnSaveGamma":
                    //{
                    //    //以文本形式保存

                    //    {//保存
                    //        SaveFileDialog sf = new SaveFileDialog(); //对话框
                    //        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                    //        string str = "灰阶序号,灰阶亮度,reg_R,reg_G,reg_B\r\n";
                    //        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                    //        {
                    //            if (sf.FileName == "") return; //如果没有输入文件名
                    //            try
                    //            {
                    //                StreamWriter sw = null; //文件流
                    //                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    //                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                    //                {
                    //                    str = str + this.GammaReg_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                    //                        + this.GammaReg_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                    //                        + this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                    //                        + this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString() + ","
                    //                        + this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString() + "\r\n";
                    //                }
                    //                sw.WriteLine(str);
                    //                sw.Dispose(); //释放资源
                    //                sw.Close(); //关闭数据流
                    //                MessageBox.Show("数据保存OK！", "系统提示：");

                    //            }
                    //            catch
                    //            {
                    //                MessageBox.Show("数据保存时发生错误，请再试！", "系统提示：");
                    //            }
                    //        }

                    //    }
                    //}
                    //        break;
                    ////    case "btnLoadGamma":
                    {
                        this.GammaReg_dataGridView.Rows.Clear();
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine(); //循环读取下一行
                                Line_Count++;
                            }
                            fs.Close(); //关闭流
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j <= Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                GammaReg_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                {
                                    GammaReg_dataGridView.Rows[i].Cells[0].Value = arrtmp[5 * (i + 1) + 0];
                                    GammaReg_dataGridView.Rows[i].Cells[1].Value = arrtmp[5 * (i + 1) + 1];
                                    GammaReg_dataGridView.Rows[i].Cells[2].Value = arrtmp[5 * (i + 1) + 2];
                                    GammaReg_dataGridView.Rows[i].Cells[3].Value = arrtmp[5 * (i + 1) + 3];
                                    GammaReg_dataGridView.Rows[i].Cells[4].Value = arrtmp[5 * (i + 1) + 4];
                                    //------------------------------------
                                    GammaValue[i, 0] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 2]);
                                    GammaValue[i, 1] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 3]);
                                    GammaValue[i, 2] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 4]);
                                }
                                MessageBox.Show("数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    //       break;
                    ////    case "btn_CAS200_Clean":
                    //        {
                    //            DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                    //            if (dr == DialogResult.OK)
                    //            {
                    //                this.GammaReg_dataGridView.Rows.Clear();
                    //            }
                    //        }
                    // //       break;
                    //
                    //    default: break;
                }
            }
        }

        private void RM692Fx_OTP_Click(object sender, EventArgs e)
        {
            RM692Fx_Reg_Data.Text = "FE,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "28,00";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            Thread.Sleep(200);
            //RM692Fx_Reg_Data.Text = "FE,00";
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //RM692Fx_Reg_Data.Text = "10,00";////是否必须下？？？？
            //RM692Fx_Others_Click(RM692Fx_RegW, null);
            //Thread.Sleep(20);
            //Thread.Sleep(200);

            ////多gamma  OTP
            if (radioButton_gamma.Checked == true)
            {
                RM692Fx_Reg_Data.Text = "FE,40";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);

                RM692Fx_Reg_Data.Text = "F0,FB";////gamma 0~8
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "EF,03";/////gamma 9~10
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "EE,38";/////gamma 20~22
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);

                RM692Fx_Reg_Data.Text = "FE,42";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "1E,01";///////gamma RAM OTP
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
            }
            /////单个gamma的OTP
            if (radioButton_gamma_single.Checked == true)
            {
                int gamma_num = 0;
                string str_gamma_num;
                gamma_num = Convert.ToInt16(RM692Fx_gamma.Text);

                RM692Fx_Reg_Data.Text = "FE,40";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                if (gamma_num <= 8)
                {
                    str_gamma_num = "F0," + (0x01 << (gamma_num - 1)).ToString("x2");
                    RM692Fx_Reg_Data.Text = str_gamma_num;
                    RM692Fx_Others_Click(RM692Fx_RegW, null);
                    Thread.Sleep(20);
                }
                else if (gamma_num <= 16)
                {
                    str_gamma_num = "EF," + (0x01 << (gamma_num - 8 - 1)).ToString("x2");
                    RM692Fx_Reg_Data.Text = str_gamma_num;
                    RM692Fx_Others_Click(RM692Fx_RegW, null);
                    Thread.Sleep(20);
                }
                else if (gamma_num <= 24)
                {
                    str_gamma_num = "EE," + (0x01 << (gamma_num - 16 - 1)).ToString("x2");
                    RM692Fx_Reg_Data.Text = str_gamma_num;
                    RM692Fx_Others_Click(RM692Fx_RegW, null);
                    Thread.Sleep(20);
                }
                else if (gamma_num <= 32)
                {
                    str_gamma_num = "ED," + (0x01 << (gamma_num - 24 - 1)).ToString("x2");
                    RM692Fx_Reg_Data.Text = str_gamma_num;
                    RM692Fx_Others_Click(RM692Fx_RegW, null);
                    Thread.Sleep(20);
                }
                else if (gamma_num <= 37)
                {
                    str_gamma_num = "EC," + (0x01 << (gamma_num - 32 - 1)).ToString("x2");
                    RM692Fx_Reg_Data.Text = str_gamma_num;
                    RM692Fx_Others_Click(RM692Fx_RegW, null);
                    Thread.Sleep(20);
                }
                else
                {
                    Thread.Sleep(20);
                }
                RM692Fx_Reg_Data.Text = "FE,42";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "1E,01";///////gamma RAM OTP
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);

            }
            //////RCN  OTP
            if (radioButton_RCN.Checked == true)
            {
                RM692Fx_Reg_Data.Text = "FE,40";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "F9,20";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
            }
            //////ALL  OTP
            if (radioButton_All.Checked == true)
            {
                RM692Fx_Reg_Data.Text = "FE,D0";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "E5,23";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "FE,40";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "F6,37";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "F7,03";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "F8,A3";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "F9,40";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "EB,03";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
            }
            /////DEMURA  OTP
            if (radioButton_Demura.Checked == true)
            {
                RM692Fx_Reg_Data.Text = "FE,40";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
                RM692Fx_Reg_Data.Text = "F7,B0";
                RM692Fx_Others_Click(RM692Fx_RegW, null);
                Thread.Sleep(20);
            }

            RM692Fx_Reg_Data.Text = "FE,40";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "F2,01";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            Thread.Sleep(200);
            RM692Fx_Reg_Data.Text = "F3,A5";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "F4,5A";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "F5,3C";
            RM692Fx_Others_Click(RM692Fx_RegW, null);
            Thread.Sleep(20);
            RM692Fx_Reg_Data.Text = "";
            Thread.Sleep(2000);
        }

        private void Btn_RM692Fx_GIR_OTP_Click(object sender, EventArgs e)
        {
            string temp0 = frmHelp.Generate_textBox.Text;
            frmHelp.Generate_textBox.Text = txtCmd_OTP.Text;
            frmHelp.Generate_File_Click(frmHelp.Generate_File_Run, null);
            frmHelp.Generate_textBox.Text = temp0;
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (RM692Fx_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else
            {
                //创建保存对话框对象
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "gamma cfg files (*.txt)|*.txt"; //过滤文件类型
                save.FilterIndex = 1;
                save.RestoreDirectory = true;
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string path = save.FileName;
                    save.DefaultExt = ".txt";
                    StreamWriter sw = new StreamWriter(path);
                    sw.WriteLine(RM692Fx_Gamma_SetBox.Text);
                    sw.Close();
                }
            }
        }
        private void 载入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "精度配置文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "gamma cfg write";

            if (of.ShowDialog() == DialogResult.OK)
            {
                RM692Fx_Gamma_SetBox.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    RM692Fx_Gamma_SetBox.AppendText(str_generate + "\r\n");
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
            }
        }

    }
}
